Infotekka Banner Logo
 
Assigning JavaScript Events to ASP Objects at Runtime
Wednesday, 16 March 2011 1718 MDT

I just hit this barrier and for a moment lapsed into a state of complete despair. I thought "surely there must be a way for me to assign my own javascript events to ASP controls that I create at runtime". I know it's possible to assign an .OnClientClick property to an ASP:Button but there is mysteriously no such property on ASP:CheckBox controls.

But I wasn't finding anything. There are a few hackish workarounds, that involve creating control trees with overlapping controls that can receive onclick events, and tricking the end user into thinking they are clicking your checkbox when in fact they are are clicking something else, that then does a bunch of voodoo behind the scenes and "solves" your problem; but looking at those solutions started making me seasick. I tend to be overly optimistic in my belief that all of these things are possible and even easy, to someone who looks hard enough.

Now the core of my dilemma is the runtime part. One of the more simple hackabout fixes involves just sticking an onclick="alert('chowder');" into the ASP:CheckBox tag on the designer form, however if you're psychotic like me and create the majority of your controls dynamically at runtime, you have no such luxury.

<ASP:CheckBox runat="server" ID="myCheckBox" onclick="alert('chowder');" />


This one works because the CheckBox control doesn't have an onclick event (like the Button does), so the ASP engine just ignores the attribute and sends it to the browser as is. Some folks who are much smarter that I am explain it pretty well here.

After some poking around with the internet without any luck, the answer struck me. The onclick attribute is just that, an attribute! So all I needed to do was use the .Attributes collection that is a part of any WebControl, since it is part of the base WebControl type.

Therefore, my solution in the end was frighteningly simple:

myCheckBox.Attributes["onclick"] = "Alert(\"chowder!\");";


By using the .Attributes collection, the attribute was handed down directly to the HTML without any tinkering from ASP. Using that, you can add just about any attribute to your HTML when it renders, all at runtime!

Please sign in to leave a comment

Other Uses - Tuesday, 14 February 2012 131 MST
I've used this in other projects lately to store info about the dynamic checkbox in a custom attribute. Like if I need to save the record ID that's related to the checkbox, I set an attribute called "recid" and store my value there. Then I can read from it on the post back and determine what's been checked!
 
Search Infotekka
 
 
User Login


 
Create an account
 
 
Infotekka SharePoint

Looking for our SharePoint development site? Look no further.

Not looking for it? Maybe you should be. Infotekka specializes in Microsoft SharePoint (2010 and 2013) architecture, installation, configuration, and most importantly customization. If you'd like to know more please send us a note from the contact page.

 
 
Changes Ahead

The time has come to retire this website and implement a new one. We're likely going to stick with the same look and feel but it's moving off of its current LAMP environment and going into .NET, where we can take advantage of all the fun integration stuff we're always crowing to our clients about.

Stay tuned for updates!

 
 
 
©2010 infotekka.com