When developing with ASP.NET server controls such as asp:DropDownList or asp:Textbox that you have declared as
runat="server", have you struggled with getting an OnChange or OnBlur event handler to be called? If you just do what you would have done with a plain old
<input type="text"> or
<select> control, which is to give the control an event attribute like
The trick is to assign the event handler to your control programmatically. Here’s an example of doing this in C# as part of the page load event:
In this example, I used the FindControl() method to find the control by id, and then I added my event handler script as an attribute. Although I passed “this” as an argument to my handler, that isn’t required. It was useful for my implementation because I used the same handler for more than one control. If you want no arguments, include the parentheses with nothing between them. Note that the handler is defined in a
<script> tag that does not say
runat="server" — you want this script to run on the client once your control’s OnChange event fires due to user interaction.