ASP.NET  

Event Handling in ASP.NET WebForms — Button Click Example

ASP.NET WebForms uses an event-driven programming model, similar to Windows applications.

When a user interacts with a control (like clicking a button), an event is triggered and ASP.NET executes the associated server-side code.

In this article, we will learn:

  • What does event handling mean in WebForms

  • How button click events work

  • Real-time business example

  • Page life cycle flow of button click

What is Event Handling in ASP.NET WebForms?

Event handling refers to the process by which user actions (events) trigger server-side methods.

Common WebForms events

ControlEvent
ButtonOnClick
TextBoxTextChanged
DropdownSelectedIndexChanged
GridViewRowCommand

WebForms automatically generates postback to the server and executes the event handler.

Button Click Event Example

ASPX (UI Design)

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

<asp:Label ID="lblMessage" runat="server"></asp:Label>

C# Code-behind (Event Handler)

protected void btnSubmit_Click(object sender, EventArgs e)
{
    lblMessage.Text = "Hello, " + txtName.Text + "! Welcome to ASP.NET WebForms.";
}

How does it work?

  1. User enters a name in the TextBox.

  2. User clicks the Submit button

  3. Page sends a PostBack request to the server

  4. The event handler executes

  5. Server returns a response with updated Label text

Real-Time Business Example

Scenario: Employee Login Page

ASPX

<asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>

C# Code

protected void btnLogin_Click(object sender, EventArgs e)
{
    if(txtEmpId.Text == "EMP001" && txtPassword.Text == "admin123")
    {
        lblStatus.Text = "Login Successful!";
        lblStatus.ForeColor = System.Drawing.Color.Green;
        // Redirect to dashboard: Response.Redirect("Dashboard.aspx");
    }
    else
    {
        lblStatus.Text = "Invalid Employee ID or Password";
        lblStatus.ForeColor = System.Drawing.Color.Red;
    }
}

PostBack & ViewState Role

FeaturePurpose
PostBackSends request back to server
ViewStateMaintains control values between requests

So after clicking the button, the page reloads but the values remain intact.

Behind the Scenes Event Sequence

Button click triggers this life cycle:

  1. Page Load

  2. PostBack event handling

  3. Button Click Event fired

  4. Render Page

  5. Response sent to browser

Conclusion

Event handling in WebForms enables:

  1. Server-side execution of user actions

  2. Easy programming model (Windows Forms style)

  3. Automatic ViewState + PostBack support

Button click is one of the most commonly used events to perform tasks like:

  • Login validation

  • Form submission

  • Calculation (e.g., tax, salary)

  • Search filters

  • Saving data to the database