Condividi tramite


Button Web Server Control Declarative Syntax

Displays a push button control on the Web Forms page.

<asp:Button
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    CommandArgument="string"
    CommandName="string"
    CssClass="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnClick="Click event handler"
    OnClientClick="string"
    OnCommand="Command event handler"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    PostBackUrl="uri"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    UseSubmitBehavior="True|False"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

The Button control allows you to create a push button on the Web Forms page. There are two types of buttons that can be created: a submit button or a command button.

By default, a Button control is a submit button. A submit button does not have a command name (specified by the CommandName property) associated with the button and simply posts the Web page back to the server. You can provide an event handler for the Click event to programmatically control the actions performed when the submit button is clicked.

A command button has a command name associated with the button (such as Sort) by setting the CommandName property. This allows you to create multiple Button controls on a Web Forms page and programmatically determine which Button control is clicked in the event handler for the Command event. You can also use the CommandArgument property with a command button to provide addition information about the command to perform, such as Ascending. You can provide an event handler for the Command event to programmatically control the actions performed when the command button is clicked.

Command buttons are frequently used in templated controls to communicate an action to the parent control, for example, selection in a DataList or changing mode in a FormView.

By default, page validation is performed when a Button control is clicked. Page validation determines whether the input controls associated with a validation control on the page pass the validation rules specified by the validation control. If you have a Button control that needs to disable this behavior, such as a reset button, set the CausesValidation property to false.

Note

Because the <asp:Button> element has no content, you can close the tag with /> instead of a separate closing tag.

To specify the caption that is displayed in the Button control, set the Text property.

For detailed information on the Button control's properties and events, see the Button documentation.

Example

The following example demonstrates the declaration for a submit button control in an .aspx file.

<asp:Button id="SubmitButton"
     Text="Submit"
     CommandName="Submit"
     OnCommand="CommandBtn_Click"
     runat="server"  />
<asp:Button id="SubmitButton"
     Text="Submit"
     CommandName="Submit"
     OnCommand="CommandBtn_Click"
     runat="server"/>

The following example demonstrates the declaration for a command button control in an .aspx file.

<asp:Button id="SortAscendingButton"
     Text="Sort Ascending"
     CommandName="Sort"
     CommandArgument="Ascending"
     OnCommand="CommandBtn_Click"
     runat="server"/>
<asp:Button id="SortAscendingButton"
     Text="Sort Ascending"
     CommandName="Sort"
     CommandArgument="Ascending"
     OnCommand="CommandBtn_Click"
     runat="server"/>

The following example shows an event-handling method that gets the button click and displays the information passed from the button in its CommandName and CommandArgument properties.

Sub CommandBtn_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
    Message.Text = "You clicked the " & e.CommandName & _
                   " - " & e.CommandArgument & " button."
End Sub
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
    Message.Text = "You clicked the " + e.CommandName +
                   " - " + e.CommandArgument + " button.";
}

See Also

Reference

Button

Other Resources

Web Server Control Syntax