Você está na página 1de 3

Button.CommandArgument Property (System.Web.UI.

WebControls)

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.butt...

Button.CommandArgument Property
.NET Framework 4
2 out of 20 rated this helpful Rate this topic

Gets or sets an optional parameter passed to the Command event along with the associated CommandName. Namespace: System.Web.UI.WebControls Assembly: System.Web (in System.Web.dll)

Syntax
C# [BindableAttribute(true)] [ThemeableAttribute(false)] public string CommandArgument { get; set; }

<asp:Button CommandArgument="String" /> Property Value Type: System.String An optional parameter passed to the Command event along with the associated CommandName. The default value is String.Empty. Implements IButtonControl.CommandArgument

Remarks
Use the CommandArgument property to specify a parameter that compliments the CommandName property.

Note
Although you can set the CommandArgument property by itself, it is normally only used when the CommandName property is also set. The CommandArgument property complements the CommandName property by allowing you to provide additional information about the command to perform. For example, if you set the CommandName property to Sort and the CommandArgument property to Ascending, you specify a command to sort in ascending order. This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

Examples
The following code example demonstrates how to use the CommandArgument property to create a Button control that sorts a list in ascending order. C# <%@ 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.

1 of 3

2/27/2012 12:26 AM

Button.CommandArgument Property (System.Web.UI.WebControls)


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>

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.butt...

Version Information
.NET Framework Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2 The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also
Reference Button Class System.Web.UI.WebControls Namespace Command OnCommand CommandName Other Resources Button Web Server Controls Content Map

Did you find this helpful?

Yes

No

2 of 3

2/27/2012 12:26 AM

Button.CommandArgument Property (System.Web.UI.WebControls)

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.butt...

Community Content
2012 Microsoft. All rights reserved.

3 of 3

2/27/2012 12:26 AM

Você também pode gostar