Calling Client Script Events in ASP.NET Server Controls
Posted by triaslama on April 25, 2008
ASP.NET has its own controls (WebControls or HtmlControls) that rendered as elements in a page. ASP.NET server controls declared with ‘asp’ tag (<asp:[control_type] />) and contains runat=”server” attribute. ASP.NET server controls has its own attribute related to a specific control (such as onclick and text for click event and button text respectively).
For example: if we want a click event associated with a button of ASP.NET server control we use this script in our ASP.NET page:
<asp:Button id="btn" onclick="btn_Click" text="Test" runat="server" />
The click event received in onclick attribute and will be handled by btn_Click method. btn_Click is a method that will be called everytime a click occurs in this button control. We can write btn_Click method with any languages that supported by .NET framework.
The important portion of source code that make calling client script events possible is the following lines of code:
Click event of ASP.NET server controls handled using server script and client script.
As an additon maybe we need this: If we include an attribute that does not map to a property of ASP.NET controls, the attribute will be ignored in server side processing and will be passed as an attribute of HTML control (rendered in client side).
Look at the following example (expage2.aspx):
Inside expage2.aspx, I put onkeydown as an attribute of ASP.NET TextBox server control. But because keydown not exists as TextBox event (please refers to .NET documentation for complete members of TextBox server control) so onkeydown just passed as a HTML control attribute.
This is result of expage2.aspx:
keydown is not a TextBox event, so onkeydown just passed and rendered as HTML control attribute.