ButtonColumn.CommandName Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene o imposta una stringa che rappresenta il comando da eseguire quando si fa clic su un pulsante nell'oggetto ButtonColumn .
public:
virtual property System::String ^ CommandName { System::String ^ get(); void set(System::String ^ value); };
public virtual string CommandName { get; set; }
member this.CommandName : string with get, set
Public Overridable Property CommandName As String
Valore della proprietà
Stringa che rappresenta il comando da eseguire quando si fa clic su un pulsante in ButtonColumn . Il valore predefinito è una stringa vuota ("").
Esempio
Nell'esempio di codice seguente viene illustrato come utilizzare la CommandName proprietà per associare un nome di comando a un pulsante. Il nome del comando del pulsante selezionato viene quindi determinato a livello di codice nel gestore eventi per l'evento ItemCommand e viene eseguita l'azione appropriata.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
Commenti
Utilizzare la CommandName proprietà per associare un nome di comando, ad esempio Add o Remove, a un pulsante. È possibile impostare la CommandName proprietà su qualsiasi stringa che identifica l'azione da eseguire quando si fa clic su un pulsante nell'oggetto System.Web.UI.WebControls.ButtonColumn . È quindi possibile determinare a livello di codice il nome del comando nel gestore eventi per l'evento ItemCommand ed eseguire le azioni appropriate.
Il valore di questa proprietà viene archiviato nello stato di visualizzazione.