Hook Client Events to Server Controls

Pick up three quick techniques to fire up server controls from the client side.

UI Tip

LANGUAGES: JavaScript | C#

TECHNOLOGIES: Client Events | Server Controls

 

Hook Client Events to Server Controls

Pick up three quick techniques to fire up server controls from the client side.

 

By Brad McCabe

 

Looking to hook client events to server controls? Well, you can take a variety of approaches. The easiest is when the control doesn't have a server event with the same name. All you need to do is put the client event on the tag:

 

<asp:Image onMouseOver="alert('Mouse Over')" ... />"

 

For objects, as a button, you can hook them to client events by adding attributes from your server-side code. This is some basic code you might add in your page load event:

 

Button1.Attributes.Add("onclick", "alert('Clicked');")

 

If you need to hook up to the form submission, or in the event of validation cases, you can use the RegisterOnSubmitStatement in your server-side code. The following page load code snippet causes any control on the page to display a JavaScript alert box before a submission or postback:

 

RegisterOnSubmitStatement("uniqueKey", "alert('Submit!');")

 

You can use any one of these strategies to call more advanced JavaScript. Using the approach that fits your needs lets you perform more logic on the client side and reduce the overhead of postbacks in ASP.NET.

 

Brad McCabe is the technical evangelist for Infragistics. Brad also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist within Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at mailto:[email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish