Asp .Net makes some things very easy. In particular, it makes adding controls that cause postbacks very very easy indeed. You can do all of your web application event handling in this way without any problems whatsoever.
Of course, you migh not want to.
So it was I came across the my little problem. Whilst its easy to program server side events and control handling in Asp .Net, information on client-side scripting whilst using Asp .Net is a little harder to come by.
document.getElementById(‘myTextBox’).value = “Value for a textbox”;
So, in order to test this I set up a little function that would check my check box and display a message if it was checked. This was testing both my ability to poll a checkbox, and my ability to have the checkbox call the event handler.
The text for the function is below:-
alert(“w00t! It worked!”);
As you can see, a very simple function. It wouldn’t work – I could not find a way to attach this function to my asp:checkbox function.
However, this raises another issue. I want to later be able to check the checkboxes from my server code. Which, strangely enough, cannot be done on html controls without some nasty text parsing which I didn’t want to get into at all. Not even a little.
So, we’re back to server controls. Here is the control definition:
<asp:CheckBox id=”checkBox1″ runat=”server”></asp:CheckBox>
I tried adding the OnCheckedChange event handler to this description, that causes an error. To be exact, it causes this error: CS0117: does not contain a definition for – I removed the identifiers there to make it easier to search for in google.
So in our code behind file, in our Page_Load function we need to add the following:-
and baboom. Thats the same as adding the onclick to the underlying html control manually. Now it works. Finally.