RGR
RGR

Reputation: 1571

How to fire both client side and server side click events of button?

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

  }

Upvotes: 1

Views: 15221

Answers (3)

Rahul R.
Rahul R.

Reputation: 6461

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.

Upvotes: 0

Joe Ratzer
Joe Ratzer

Reputation: 18549

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?");
}

Upvotes: 9

Erhan Akpınar
Erhan Akpınar

Reputation: 116

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

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

}

Upvotes: 0

Related Questions