The Button Web control provides a generic pushbutton-style control that causes the Web Forms page to be posted back to the server.
Required properties are noted in boldface type.
<asp:Button id="MyButton" runat="server" Text="label" Command="command" CommandArgument="commandArgument" OnClick="OnClickMethod" />
Note Because the <asp:Button> element has no content, you can close it with />.
Property | Description |
---|---|
(Base control properties) | The properties defined in Base Web Control Properties. |
CommandArgument | Optional command argument used in conjunction with the value of the Command property. Contains a value that should be bubbled when a button is clicked. For example, if the Command property is set to "Sort," the CommandArgument property might be set to "Ascending" or "Descending." |
Command | Command that should be bubbled when a button embedded in a container control is clicked. The value set in this property is available as an argument of the OnItemCommand event of the container control. For example, if the Button control is in a DataList control, the Command might be set to "Sort." You can pass additional information using the CommandArgument property. |
Text | The button caption. |
Event (and paramters) | Description |
---|---|
OnClick(Object sender, EventArgs e) | Raised when the user clicks the button. This event always causes the page to be posted to the server.
The e events argument object has no properties. |
The following shows a sample declaration for a Button control in an .aspx file. The button is embedded in a Repeater control; its OnClick event will be bubbled to the container control, where it will raise an OnItemCommand event. The CommandArgument property is set using a data-binding expression that gets the value of a field called Ticker.
[C#]
<asp:Button id=btnBuy runat="server" Text="Buy" Command="buy" CommandArgument="<%# ((PositionData)Container.DataItem).Ticker %>" />
The following shows an event-handling method on the Repeater control that gets the button click and displays the information passed from the button from its Command and CommandArgument properties.
[C#]
protected void Repeater1_ItemCommand(object sender,
RepeaterCommandEventArgs e)
{
string cmd = ((Button)e.CommandSource).Command;
string arg = ((Button)e.CommandSource).CommandArgument;
lblResult.Text = "<u>Command</u> is: <b>" + cmd + "</b>,
<u>CommandArgument</u> is: <b>" + arg + "</b>";
}
See Also