ASP.NET Tutorials
Sheo Narayan


Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:Button control
Button control is used to post the form or fire an event either client side or server side.
Button control is generally used to post the form or fire an event either client side or server side. When it is rendered on the page, it is generally implemented through <input type=submit> HTML tag. However, if UserSubmitBehavior property is set to false then control will render out as <input type=button>.

Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <input> tag. You can set its Text property either by setting Text property in the .aspx page or from server side page. (other properties can also be set from both pages)

A button can be set as default button (Default button is a button that is Clicked when the user presses Enter key while filling a form.) by specifying DefaultButton property of the <form> tag or to the asp:Panel tag in which it is placed. eg. If we have to set a button with id btnSubmit as default button we can add following properties to the <form> element. <form id="Form1" DefaultButton="btnSubmit" runat="server">.

Following are some important properties that are very useful.
UserSubmitBehavior true/false. If true, the button will be used as client browser submit mechanism else postback mechanism.
CausesValidation Value can be set as true/false. This indicates whether validation will be performed when a button is clicked.
PostBackUrl Indicates the URL on which the Form will be posted back.
ValidationGroup Gets or Sets the name of the validation group that the button belongs to. This is used to validate only a set of Form controls with a Button.
OnClick Attach a server side method that will fire when button will be clicked.
OnClientClick Attach a client side (javascript) event that will fire when button will be clicked.
DEMO : Button Show Source Code
  Ex. Example of Label Control
                // With OnClick event
                <asp:Button ID="btnServer" runat="Server" Text="Activate Server Side Event" OnClick="ActivateServerSideEvent" />
                // with OnClientClick event and CommandName 
                <asp:Button ID="btnClient" runat="Server" Text="Activate Client Side Method" OnClientClick="GiveAlertToUser()" UseSubmitBehavior="False" CommandName="ClientSideButton" />
                // with OnClick & ValidationGroup
                <asp:Button ID="Button1" runat="Server" OnClick="FireServerSideEvent" Text="Fire Server Side Event" ValidationGroup="demo" />
Here note that when you click "Fire Server Side Event" button, the form will be posted only when you have entered something into the TextBox, while other buttons are posted back without validatation. Here only textbox and "Fire Server Side Event" button have same ValidateionGroup called "demo", so this button is validating only textbox.