You can create event-handling methods and server control events to them using ASP+ syntax. In order to do so, you must know:
To create event handlers using ASP+ syntax
<asp:Button id="Button1" runat="SERVER" OnClick="MyHandler" />
Note Remember that any control must be between opening and closing tags of an HtmlForm control, <form runat="server"></form>
.
[Visual Basic]
Private Sub methodname(sender As Object, e as EventArgs)
In C#, it must have a signature such as the following:
[C#]
protected void methodname (object sender, EventArgs e)
Note The type of the second argument depends on the control for which you are creating a handler. For example, the OnVisibleMonthChanged event of the Calendar Web control requires an argument derived from the MonthChangedEventArgs class. For details, refer to the reference documentation for the control you are working with.
The following example shows a simple but complete file that contains a button and a label. The button's OnClick event is bound to the Button1_OnClick method, which displays text in the label.
<%@ Page Language="C#" %> <%@ Import Namespace="System.Collections" %> <HTML> <HEAD> <SCRIPT RUNAT="SERVER"> protected void Button1_OnClick(object Source, EventArgs e){ Label1.Text="You clicked the button"; } </SCRIPT> </HEAD> <BODY> <H3>Simple <asp:Button> Sample</H3> <FORM RUNAT=SERVER> <asp:Button id=Button1 runat="server" Text="Button1" onclick="Button1_OnClick" /> <asp:Label id=Label1 runat=server /> </FORM> </BODY> </HTML>
See Also