0

I want to call C# code in the code behind from JavaScript ONLY when the validation in the JavaScript is true. I am facing some issues with this can someone help. Thanks Here is my JavaScript

  <script type="text/javascript">
        function Validate_Checkbox() {
            var chks = $("#<%= cblCustomerList.ClientID %> input:checkbox");

            var hasChecked = false;
            for (var i = 0; i < chks.length; i++) {
                if (chks[i].checked) {
                    hasChecked = true;
                 //call C# code
                    break;
                }
            }
            if (hasChecked == false) {
                alert("Please select at least one checkbox..!");

                return false;
            }

            return true;
        }     
    </script>

here is the C# code that i want to call

 protected void DV_Test_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
       do something
    }

Here is the button that calls the JavaScript

<asp:Button ID="Button1" runat="server" CausesValidation="True" 
                            CommandName="Insert" Text="Insert" OnClientClick="javascript:Validate_Checkbox();return true;"/>
moe
  • 5,149
  • 38
  • 130
  • 197
  • You can add hidden button and bind some action you want to execute in `C#` code. And click this button after validation from `javascript`. It will cause postback ofc. – ChruS Mar 07 '13 at 16:10
  • You can found an answer in http://michbex.com/wordpress/?p=66 – Mihai8 Mar 07 '13 at 16:14
  • i have looked all these suggestions but don't seem to find something that is easier so i can follow it. my experience is javascript and c# is limited. i will try to dig it some more info. – moe Mar 07 '13 at 16:30

5 Answers5

1

Here the answer to your question How to make an AJAX call without jQuery?

You might think however, in using JQuery which will simplify the way to write javascript code.

Community
  • 1
  • 1
simple-thomas
  • 671
  • 7
  • 20
0

Here is a very good explanation on this topic

1: http://www.dotnetcurry.com/ShowArticle.aspx?ID=109&AspxAutoDetectCookieSupport=1

2: http://forums.asp.net/t/1111231.aspx

I hope this help you ;-)

Or if you have the chance to use the library [Telerik] for ASP. You can call C # code easily with the controller [RadAjaxManager]

Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
0

we can use Ajax Call with data type

1) json , in this case you will create web method on the code behind and return string to JavaScript as return result

2) HTML , in this case you will call new HTML page and return HTML

0

I think that you should use server side event (Click) with combination of client side event. On the server side:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

and in the code behind

protected void Button1_Click(object sender, EventArgs e)
{
    //do something here
}

Additionally in your javascript code you should use event.preventDefault() if certain conditions are not met. Please check Mozilla docs for details. The sample code on your client side could looks like (Not tested!):

$(document).ready(function(){
  $('#Button1').click(function(e){
    //some logic to check checkbox status and call e.preventDefault();
  });
});
Slawomir Pasko
  • 907
  • 8
  • 14
  • i want to make sure the Button1_Click fires only when the validation is true. I am not sure where to put that in my javascript. my coding experience is very limited so please help if u can – moe Mar 07 '13 at 16:28
  • i have tried this but it is not working for me can u please provide a working example? thanks – moe Mar 07 '13 at 21:18
0

You need to use Eval. Example:

<script type="text/javascript">
    function Validate_Checkbox() {
        ...
        if (chks[i].checked) {
            hasChecked = true;
            eval("<%= Page.ClientScript.GetPostBackEventReference(lbSubmit, String.Empty) %>");
        }
        ...
    }

<asp:Button ID="Button1" CausesValidation="True"  CommandName="Insert" Text="Insert" OnClientClick="javascript:Validate_Checkbox();return true;" runat="server"/>

<asp:LinkButton ID="lbSubmit" style="display:none" OnClick="slbCheckVin_Click" runat="server"/>
AlphaOmega
  • 700
  • 10
  • 11