Export (0) Print
Expand All
1 out of 10 rated this helpful - Rate this topic

Button Class

Displays a push button control on the Web page.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class Button : WebControl, IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
Not applicable.

Use the Button control to create a push button on the Web page. You can create either 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 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.

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 all pass the validation rules specified by the validation control. To prevent page validation from occurring, set the CausesValidation property to false.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Respond to Button Web Server Control Events in Client ScriptBuilding ASP .NET Web Applications
How to: Connect Multiple Events to a Single Event Handler in ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Determine which Web Server Control Raised an EventBuilding ASP .NET Web Applications
How to: Add Button Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
Walkthrough: Creating a Basic Control Designer for an ASP.NET Web Server ControlAuthoring ASP.NET Controls
How to: Add PlaceHolder Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Respond to Button Web Server Control EventsBuilding ASP .NET Web Applications
How to: Respond to Button Events in Data-Bound ControlsBuilding ASP .NET Web Applications
How to: Respond to Button Events in DataList, Repeater, or GridView ItemsBuilding ASP .NET Web Applications
Walkthrough: Creating a Basic Control Designer for an ASP.NET Web Server ControlAuthoring ASP.NET Controls
How to: Respond to Button Web Server Control Events in Client ScriptBuilding ASP .NET Web Applications
How to: Connect Multiple Events to a Single Event Handler in ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Determine which Web Server Control Raised an EventBuilding ASP .NET Web Applications
How to: Add Button Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Add PlaceHolder Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Respond to Button Web Server Control EventsBuilding ASP .NET Web Applications
How to: Respond to Button Events in Data-Bound ControlsBuilding ASP .NET Web Applications
How to: Respond to Button Events in DataList, Repeater, or GridView ItemsBuilding ASP .NET Web Applications
Walkthrough: Working with Cascading Style Sheet Styles in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating Reusable Elements with ASP.NET User ControlsBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Validating User Input in a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio
How to: Create Event Handlers in ASP.NET Web Pages (Visual Studio)Building ASP .NET Web Applications in Visual Studio
Walkthrough: Data Binding to a Custom Business ObjectBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Basic Web Page in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
How to: Add Button Web Server Controls to a Web Forms Page (Visual Studio)Building ASP .NET Web Applications in Visual Studio
Walkthrough: Customizing a Web Site Using Themes in Visual StudioBuilding ASP .NET Web Applications in Visual Studio
How to: Add Button Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio

The following code example demonstrates how to create a Submit Button control that posts the Web page content back to the server.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Button Example</title>
<script language="C#" runat="server">

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

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

      <h3>Button Example</h3>

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

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

The following code example demonstrates how to create a Command Button control that sorts a list.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button CommandName Example</title>
<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 id="form1" 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>


System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Button
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.