ButtonColumn.CommandName-Eigenschaft
Ruft eine Zeichenfolge ab, die den Befehl darstellt, der beim Klicken auf eine Schaltfläche im System.Web.UI.WebControls.ButtonColumn-Objekt ausgeführt werden soll, oder legt diese Zeichenfolge fest.
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)
Syntax
'Declaration
Public Overridable Property CommandName As String
'Usage
Dim instance As ButtonColumn
Dim value As String
value = instance.CommandName
instance.CommandName = value
public virtual string CommandName { get; set; }
public:
virtual property String^ CommandName {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_CommandName ()
/** @property */
public void set_CommandName (String value)
public function get CommandName () : String
public function set CommandName (value : String)
Eigenschaftenwert
Eine Zeichenfolge ab, die den Befehl darstellt, der beim Klicken auf eine Schaltfläche in ButtonColumn ausgeführt werden soll. Der Standardwert ist eine leere Zeichenfolge ("").
Hinweise
Mithilfe der CommandName-Eigenschaft können Sie einer Schaltfläche einen Befehlsnamen, z. B. Add oder Remove, zuordnen. Sie können die CommandName-Eigenschaft auf eine beliebige Zeichenfolge festlegen, die die beim Klicken auf eine Schaltfläche im System.Web.UI.WebControls.ButtonColumn-Objekt auszuführende Aktion angibt. Anschließend können Sie im Ereignishandler des ItemCommand-Ereignisses den Befehlsnamen programmgesteuert bestimmen und die entsprechenden Aktionen ausführen.
Der Wert dieser Eigenschaft wird im Ansichtszustand gespeichert.
Beispiel
Im folgenden Codebeispiel wird veranschaulicht, wie mithilfe der CommandName-Eigenschaft einer Schaltfläche ein Befehlsname zugeordnet wird. Anschließend wird der Befehlsname der Schaltfläche, auf die geklickt wurde, im Ereignishandler des ItemCommand-Ereignisses programmgesteuert bestimmt und die entsprechende Aktion ausgeführt.
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
' Create dynamic column to add to Columns collection.
Dim AddColumn As New ButtonColumn
AddColumn.HeaderText = "Add Item"
AddColumn.Text = "Add"
AddColumn.CommandName = "Add"
AddColumn.ButtonType = ButtonColumnType.PushButton
' Add column to Columns collection.
ItemsGrid.Columns.AddAt(0, AddColumn)
End Sub
Protected Sub ItemsGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
If e.CommandName = "Add" Then
' Add logic for addition operation here.
TextBox1.Text = "Added"
End If
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<asp:DataGrid
ID="ItemsGrid"
runat="server"
DataSourceID="CustomersSqlDataSource"
AutoGenerateColumns="true"
OnItemCommand="ItemsGrid_ItemCommand" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
runat="server">
</asp:sqldatasource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Init(object sender, EventArgs e)
{
// Create dynamic column to add to Columns collection.
ButtonColumn AddColumn = new ButtonColumn();
AddColumn.HeaderText="Add Item";
AddColumn.Text="Add";
AddColumn.CommandName="Add";
AddColumn.ButtonType = ButtonColumnType.PushButton;
// Add column to Columns collection.
ItemsGrid.Columns.AddAt(0, AddColumn);
}
protected void ItemsGrid_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "Add")
{
// Add logic for addition operation here.
this.TextBox1.Text = "Added";
}
}
</script>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<asp:DataGrid
ID="ItemsGrid"
runat="server"
DataSourceID="CustomersSqlDataSource"
AutoGenerateColumns="true"
OnItemCommand="ItemsGrid_ItemCommand" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
runat="server">
</asp:sqldatasource>
</div>
</form>
</body>
</html>
Plattformen
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
ButtonColumn-Klasse
ButtonColumn-Member
System.Web.UI.WebControls-Namespace
ItemCommand
String.Empty