Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Button Class

Displays a push button control on the Web page.

For a list of all members of this type, see Button Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Button

[Visual Basic]
Public Class Button
   Inherits WebControl
   Implements IPostBackEventHandler
[C#]
public class Button : WebControl, IPostBackEventHandler
[C++]
public __gc class Button : public WebControl, IPostBackEventHandler
[JScript]
public class Button extends WebControl implements
   IPostBackEventHandler

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use the Button control to create a push button on the Web page. You can create either a submit button or a command 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. By default, a Button control is a submit button. 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 page and programmatically determine which Button control is clicked. You can also use the CommandArgument property with a command button to provide additional 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.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to create a submit Button control that posts the Web page content back to the server.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
   <script language="VB" runat=server>
    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
        Message.Text = "Hello World!!"
    End Sub 'SubmitBtn_Click
  </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
   <script language="C#" runat=server>

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>
   <script language="JScript" runat=server>

      function SubmitBtn_Click(sender : Object, e : EventArgs) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>

[Visual Basic, C#, JScript] The following example demonstates how to create a sort command Button control that sorts a list.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 

         Select e.CommandName

            Case "Sort"

               ' Call the method to sort the list.
               Sort_List(CType(e.CommandArgument, String))

            Case "Submit"

               ' Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button"

               ' Test whether the command argument is an empty string ("").
               If CType(e.CommandArgument , String) = "" Then
              
                  ' End the message.
                  Message.Text &= "."
               
               Else
               
                  ' Display an error message for the command argument. 
                  Message.Text &= ", however the command argument is not recogized."
               
               End If                

            Case Else

               ' The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized."

         End Select

      End Sub

      Sub Sort_List(commandArgument As String)

         Select commandArgument

            Case "Ascending"
 
               ' Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button."

            Case "Descending"
              
               ' Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button."

            Case Else
        
               ' The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized."

         End Select

      End Sub

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

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

      &nbsp;

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

      <br><br>

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

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br><br>

      <asp:Label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void CommandBtn_Click(Object sender, CommandEventArgs e) 
      {

         switch(e.CommandName)
         {

            case "Sort":

               // Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               break;

            case "Submit":

               // Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               // Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               {
                  // End the message.
                  Message.Text += ".";
               }
               else
               {
                  // Display an error message for the command argument. 
                  Message.Text += ", however the command argument is not recogized.";
               }                
               break;

            default:

               // The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               break; 

         }

      }

      void Sort_List(string commandArgument)
      {

         switch(commandArgument)
         {

            case "Ascending":
 
               // Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               break;

            case "Descending":
              
               // Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               break;

            default:
        
               // The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               break;

         }

      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

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

      &nbsp;

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

      <br><br>

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

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br><br>

      <asp:Label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

Button Members | System.Web.UI.WebControls Namespace | CommandName | CommandArgument | Click | Command

Show:
© 2015 Microsoft