NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Writing Event Handlers for Web Forms Controls

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

  1. In the declarative tag for the control, assign a method name to the name of the event:
    <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>.
  2. Create an event-handling method in your page code. The method can be named anything, but must have a signature similar to the following:
    [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 &lt;asp:Button&gt; Sample</H3>
    <FORM RUNAT=SERVER>
        <asp:Button id=Button1 runat="server"
           Text="Button1"
           onclick="Button1_OnClick" />
        &nbsp;&nbsp;
        <asp:Label id=Label1 runat=server />
    </FORM>
</BODY>
</HTML>

See Also

Web Forms Server Controls |