ButtonField.CommandName Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u een tekenreeks op die de actie vertegenwoordigt die moet worden uitgevoerd wanneer op een knop in een ButtonField object wordt geklikt.
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
Waarde van eigenschap
De naam van de actie die moet worden uitgevoerd wanneer op een knop in de ButtonField knop wordt geklikt.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de CommandName eigenschap gebruikt om een opdrachtnaam op te geven voor de knoppen in een ButtonField object van een GridView besturingselement.
<%@ 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">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected author.
Message.Text = "You selected " + contact + ".";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ButtonField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"
AssociatedControlID="CustomersGridView"/>
<!-- Populate the Columns collection declaratively. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="ContactName"
headertext="Contact Name"/>
</columns>
</asp:gridview>
<!-- 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>
</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">
Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple ButtonField column fields are used, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Select" Then
' Convert the row index stored in the CommandArgument
' property to an Integer.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Get the last name of the selected author from the appropriate
' cell in the GridView control.
Dim selectedRow As GridViewRow = CustomersGridView.Rows(index)
Dim contactCell As TableCell = selectedRow.Cells(1)
Dim contact As String = contactCell.Text
' Display the selected author.
Message.Text = "You selected " & contact & "."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ButtonField Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"
AssociatedControlID="CustomersGridView"/>
<!-- Populate the Columns collection declaratively. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="ContactName"
headertext="Contact Name"/>
</columns>
</asp:gridview>
<!-- 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>
</form>
</body>
</html>
Opmerkingen
Gebruik de CommandName eigenschap om een opdrachtnaam, zoals "Add" of "Remove", te koppelen aan de knoppen in het ButtonField object. U kunt de CommandName eigenschap instellen op een tekenreeks die de actie identificeert die moet worden uitgevoerd wanneer op de opdrachtknop wordt geklikt. Vervolgens kunt u programmatisch de opdrachtnaam in een gebeurtenishandler bepalen en de juiste acties uitvoeren.
Note
Alle knoppen in een ButtonField object delen dezelfde opdrachtnaam.
Met gegevens gebonden besturingselementen worden bepaalde opdrachtnamen herkend en worden de juiste gebeurtenissen voor het besturingselement automatisch gegenereerd en verwerkt. De volgende opdrachtnamen worden herkend:
"Cancel""Delete""Edit""Insert""New""Page""Select""Sort""Update"
Als u paging wilt aanroepen, stelt u het CommandName besturingselement in "Page" op en het CommandArgument ingesloten Button besturingselement op "First", "Last""Prev"of "Next"op een paginanummer. Omdat het CommandArgument voor een ButtonField besturingselement echter altijd de gehele rijindex is, is een ButtonField besturingselement niet geschikt voor het aanroepen van paging. Op dezelfde manier kunt u sorteren aanroepen door de CommandName instelling in te "Sort"stellen, is de CommandArgument eigenschap voor een ButtonField besturingselement altijd de index voor gehele getallen. Daarom is een ButtonField besturingselement niet geschikt voor het aanroepen van sortering. Voor aangepaste opdrachtnamen, zoals "Add" en "Remove", moet u de gebeurteniscode schrijven om de opdrachtnaam te controleren en een aangepaste actie uit te voeren. Zie DataControlCommands voor meer informatie.
Note
Niet alle opdrachtnamen worden herkend door alle gegevensgebonden besturingselementen. Wordt bijvoorbeeld "New" niet herkend door het GridView besturingselement en "Select" wordt niet herkend door het DetailsView besturingselement.
De waarde van deze eigenschap wordt opgeslagen in de weergavestatus.