Button (Clase)
Ensamblado: System.Web (en system.web.dll)
Utilice el control Button para crear un botón de comando en la página Web. Se puede crear un botón Enviar o un botón Comando.
De manera predeterminada, un control Button es un botón Enviar. Un botón Enviar no tiene asociado un nombre de comando (especificado por la propiedad CommandName) y simplemente devuelve la página Web al servidor. Se puede proporcionar un controlador de eventos para el evento Click con el fin de controlar mediante programación las acciones realizadas cuando se hace clic en el botón Enviar.
Un botón Comando puede tener asociado un nombre de comando, como Sort, mediante el establecimiento de la propiedad CommandName. Esto permite crear varios controles Button en una página Web y determinar mediante programación en qué control Button se hace clic. También se puede usar la propiedad CommandArgument con un botón de comando para facilitar información adicional sobre el comando que se va a ejecutar, como Ascending. Se puede proporcionar un controlador de eventos para el evento Command con el fin de controlar mediante programación las acciones realizadas cuando se hace clic en el botón Comando.
De manera predeterminada, se realiza la validación de la página cuando se hace clic en un control Button. La validación de la página determina si todos los controles de entrada asociados a un control de validación en la página cumplen las reglas de validación especificadas por el control de validación. Para evitar que se realice la validación de la página, establezca la propiedad CausesValidation en false.
Accesibilidad
Es posible que el marcado que se representa de manera predeterminada para este control no respete los estándares de accesibilidad enunciados en las directrices prioritarias de WCAG (Instrucciones de accesibilidad a contenido Web 1.0). Para obtener detalles sobre accesibilidad compatible con este control, vea Controles y accesibilidad en ASP.NET.
En el ejemplo de código siguiente se muestra cómo crear un control Button de envío que devuelve el contenido de la página Web al servidor.
<%@ 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>
En el ejemplo de código siguiente se muestra cómo crear un control Button de comando que ordena una lista.
<%@ 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"/> <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"/> <asp:Button id="Button4" Text="Unknown Command Name" CommandName="UnknownName" CommandArgument="UnknownArgument" OnCommand="CommandBtn_Click" runat="server"/> <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>
- AspNetHostingPermission Para trabajar en un entorno alojado en host. Valor de la petición: LinkDemand; valor del permiso: Minimal
- AspNetHostingPermission Para trabajar en un entorno alojado en host. Valor de la petición: InheritanceDemand; valor del permiso: Minimal
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.Button
Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter
Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.