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!
Looking for our SharePoint development site? Look no further.
Not looking for it? Maybe you should be. Infotekka specializes in Microsoft SharePoint 2010 architecture, installation, configuration, and most importantly customization. If you'd like to know more please send us a note from the contact page.
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!