Click Event
Collapse the table of content
Expand the table of content

Button.Click Event


Occurs when the Button control is clicked.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public event EventHandler Click

The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button).

For more information about handling events, see NIB: Consuming Events.

The following code example demonstrates how to specify and code an event handler for the Click event in order to display a message on the Web page when the Button control is clicked.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

    void Page_Load(Object sender, EventArgs e)
        // Manually register the event-handling method for
        // the Click event of the Button control.
        Button1.Click += new EventHandler(this.GreetingBtn_Click);

    void GreetingBtn_Click(Object sender,
                           EventArgs e)
        // When the button is clicked,
        // change the button text, and disable it.

        Button clickedButton = (Button)sender;
        clickedButton.Text = "...button clicked...";
        clickedButton.Enabled = false;

        // Display the greeting label text.
        GreetingLabel.Visible = true;


<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
      <h3>Simple Button Example</h3>

      <asp:Button id="Button1"
           Text="Click here for greeting..."
      <br />
      <br />
      <asp:Label ID="GreetingLabel" runat="server" 
                 Visible="false" Text="Hello World!" />

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft