Button OnClick() method and click event
The Button is an asp.net web server control. This control displays a
push button control on the web page. Button server control exists under
System.Web.UI.WebControls namespace. Button control allows the users to post a
page to the web server. By default, a Button control is a submit button.
Button OnClick() method raises the click event of the Button control.
The Button Click event occurs when the button control is clicked. The click event is commonly used when button control has no associate command name such as a submit button. We can specify a Button's command name by its CommandName property. Submit Button does not have a command name and it simply posts the page back to the web server. We can write an event handler for the click event to programmatically perform any action when submit Button is clicked.
The following asp.net c# example code demonstrates to us how can we use click event in button control.
In this example code, we created two Buttons and Label control. Both buttons have a click event and we provided the click event handler for both of them. When someone clicks any one Button then the specified Button’s click event raise and after posting back the Label control display which Button is clicked.
Button OnClick() method raises the click event of the Button control.
The Button Click event occurs when the button control is clicked. The click event is commonly used when button control has no associate command name such as a submit button. We can specify a Button's command name by its CommandName property. Submit Button does not have a command name and it simply posts the page back to the web server. We can write an event handler for the click event to programmatically perform any action when submit Button is clicked.
The following asp.net c# example code demonstrates to us how can we use click event in button control.
In this example code, we created two Buttons and Label control. Both buttons have a click event and we provided the click event handler for both of them. When someone clicks any one Button then the specified Button’s click event raise and after posting back the Label control display which Button is clicked.
ButtonOnClickExample.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = "You clicked the first button.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
Label1.Text = "You clicked the second button.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to use Button Click event in asp.ent</title>
</head>
<body style="padding:25px">
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
Button OnClick() method and click event
</h2>
<hr width="450" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="X-Large"
ForeColor="Crimson"
Font-Italic="true"
/>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="First Button"
OnClick="Button1_Click"
Font-Bold="true"
ForeColor="DodgerBlue"
Height="45"
Width="150"
/>
<asp:Button
ID="Button2"
runat="server"
Text="Second Button"
OnClick="Button2_Click"
Font-Bold="true"
ForeColor="DodgerBlue"
Height="45"
Width="150"
/>
</div>
</form>
</body>
</html>

