admin管理员组

文章数量:1277377

For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. Is that possible? I have tried like the below. But it does not fire client side click event.

[ASPX]

  <asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable"  CausesValidation="false"  OnClientClick="return click();" />

[Script]

 function click()
 {
        alert("hi")

  }

For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. Is that possible? I have tried like the below. But it does not fire client side click event.

[ASPX]

  <asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable"  CausesValidation="false"  OnClientClick="return click();" />

[Script]

 function click()
 {
        alert("hi")

  }
Share Improve this question asked Jan 22, 2013 at 11:27 RGRRGR 1,5712 gold badges23 silver badges37 bronze badges 5
  • your code seems ok it should work. Can you tell the behavior you are getting after button clicked. – Gaurav Rajput Commented Jan 22, 2013 at 11:39
  • It fires Server side click event. But not client side event. – RGR Commented Jan 22, 2013 at 11:40
  • All events are client side. There's no such thing as a server-side event. ASP's syntax just makes it appear that way. One of the many reasons, I avoid ASP. – Beetroot-Beetroot Commented Jan 22, 2013 at 11:46
  • Yes. The error is in my code!. I specified client-side event as 'click();' . That's the mistake i had done. Thanks for your much help anyways :). I feel like silly now!! – RGR Commented Jan 22, 2013 at 12:02
  • I have renamed the client-side click event as 'disable()' and it works now. Thanks all for your help. – RGR Commented Jan 22, 2013 at 12:08
Add a ment  | 

3 Answers 3

Reset to default 9

Yes, it's possible. If the client side Javascript returns true then the server-side method will get called.

For example:

function click()
{
    return confirm("Do you want to continue?");
}
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable"  CausesValidation="false"  OnClientClick="click();" />

function click()
{
   alert("hi");
   this.click();

}

The client side function must be called in your code. Only thing is depending on what it returns the server side function will get called:

   function Click() {
    alert('Hi');
    if (somecondition) {
        return true;
    }
    return false;
}

Also check your server side event handler if it is named correct as you mention in your code "disable". I think this isnt proper name.

Also check if you really need CausesValidation attribute? as this would have been for your validater which might no longer needed as client side function is manually called.

本文标签: javascriptHow to fire both client side and server side click events of buttonStack Overflow