Jquery asp.net Button Click Event via ajax

后端 未结 5 798
情话喂你
情话喂你 2020-12-12 17:13

I was wondering if anyone can point me in the right direction. I have an asp.net button with a click event (that runs some server side code). What i\'d like to do is call t

5条回答
  •  半阙折子戏
    2020-12-12 17:47

    In the client side handle the click event of the button, use the ClientID property to get he id of the button:

    $(document).ready(function() {
    $("#<%=myButton.ClientID %>,#<%=muSecondButton.ClientID%>").click(
    
        function() {
         $.get("/myPage.aspx",{id:$(this).attr('id')},function(data) {
           // do something with the data
         return false;
         }
        });
     });
    

    In your page on the server:

    protected void Page_Load(object sender,EventArgs e) {
     // check if it is an ajax request
     if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") {
      if (Request.QueryString["id"]==myButton.ClientID) {
        // call the click event handler of the myButton here
        Response.End();
      }
      if (Request.QueryString["id"]==mySecondButton.ClientID) {
        // call the click event handler of the mySecondButton here
        Response.End();
      }
     }
    }
    

提交回复
热议问题