ButtonField.CommandName Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur string yang mewakili tindakan yang akan dilakukan saat tombol dalam objek diklik ButtonField .
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
Nilai Properti
Nama tindakan yang akan dilakukan saat tombol di diklik ButtonField .
Contoh
Contoh kode berikut menunjukkan cara menggunakan CommandName properti untuk menentukan nama perintah untuk tombol dalam ButtonField objek GridView kontrol.
<%@ 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>
Keterangan
CommandName Gunakan properti untuk mengaitkan nama perintah, seperti "Add" atau "Remove", dengan tombol dalam ButtonField objek. Anda dapat mengatur CommandName properti ke string apa pun yang mengidentifikasi tindakan yang akan dilakukan saat tombol perintah diklik. Anda kemudian dapat menentukan nama perintah secara terprogram dalam penanganan aktivitas dan melakukan tindakan yang sesuai.
Nota
Semua tombol dalam objek memiliki ButtonField nama perintah yang sama.
Kontrol terikat data mengenali nama perintah tertentu dan secara otomatis menaikkan dan menangani peristiwa yang sesuai untuk kontrol. Nama perintah berikut dikenali:
"Cancel""Delete""Edit""Insert""New""Page""Select""Sort""Update"
Untuk memanggil penomoran halaman, atur CommandName ke "Page" dan CommandArgument dari kontrol yang terkandung Button ke "First", , "Last", "Prev""Next", atau nomor halaman. Namun, karena CommandArgument untuk ButtonField kontrol selalu merupakan indeks baris bilangan bulat, ButtonField kontrol tidak cocok untuk memanggil penomor. Demikian pula, meskipun Anda dapat memanggil pengurutan dengan mengatur CommandName ke "Sort", CommandArgument properti untuk ButtonField kontrol selalu merupakan indeks baris bilangan bulat. Untuk alasan ini, ButtonField kontrol tidak cocok untuk memanggil pengurutan. Untuk nama perintah kustom, seperti "Add" dan "Remove", Anda perlu menulis kode peristiwa untuk memeriksa nama perintah dan melakukan beberapa tindakan kustom. Untuk informasi selengkapnya, lihat DataControlCommands .
Nota
Tidak semua nama perintah dikenali oleh setiap kontrol terikat data. Misalnya, "New" tidak dikenali oleh GridView kontrol dan "Select" tidak dikenali oleh DetailsView kontrol.
Nilai properti ini disimpan dalam status tampilan.