简体   繁体   中英

How to call C# Code from JavaScript

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;"/>

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.

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 ]

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

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();
  });
});

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM