Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Creating Both Client-Side and Server-Side Click Event Handlers

  Asked By: Bryant    Date: Nov 10    Category: Asp.net    Views: 3130
  

I'm trying to get a button to fire off both client-side javascript
and some server-side code, but I can only get it to do one or the
other. If I set both up, only the client-side event-handler will
fire. I've played with using different events (e.g. onmousedown for
the client-side event and onclick for the server-side event), but it
doesn't seem to make any difference.

I've read lots of articles telling me I can do this, but they only
contain examples of one or the other. What am I missing here?

ASPX File


...
<script language="javascript">
function ClientClick() {
alert("this is client code.");
}
...

ASPX.CS File

MyButton_Load(...) {
MyButton.Attributes["onclick"] = "ClientClick();";
}

MyButton_Click(...) {
// server side code goes here.
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Esayas Beshah     Answered On: Jan 27


Try this code sample:
<asp:Button ID="cmdIvoke" runat="server" onclick="cmdIvoke_Click" OnClientClick="alert('client side invoked');return true;" Text="Invoke Both" />

You can control the execution of the server side code by the return value, true means continue executing the server side handler code.

 




Tagged: