当单击 GridView控件中的按钮时发生。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
在单击 GridView 控件中的按钮时,将引发 RowCommand 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程。
GridView 控件中的按钮也可调用该控件的某些内置功能。若要执行这些操作之一,请将按钮的 CommandName 属性设置为下表中的某个值。
CommandName 值 |
说明 |
---|---|
“Cancel” |
取消编辑操作并将 GridView 控件返回为只读模式。引发 RowCancelingEdit 事件。 |
“Delete” |
删除当前记录。引发 RowDeleting 和 RowDeleted 事件。 |
“Edit” |
将当前记录置于编辑模式。引发 RowEditing 事件。 |
“Page” |
执行分页操作。将按钮的 CommandArgument 属性设置为“First”、“Last”、“Next”、“Prev”或页码,以指定要执行的分页操作类型。引发 PageIndexChanging 和 PageIndexChanged 事件。 |
“Select” |
选择当前记录。引发 SelectedIndexChanging 和 SelectedIndexChanged 事件。 |
“Sort” |
|
“Update” |
更新数据源中的当前记录。引发 RowUpdating 和 RowUpdated 事件。 |
尽管单击上表中所列出的按钮时将引发 RowCommand 事件,但仍建议您使用该表中列出的事件来执行该操作。
将 GridViewCommandEventArgs 对象传递到事件处理方法,以便您可以确定被单击按钮的命令名和命令参数。
注意 |
---|
GridViewCommandEventArgs 类未包含一个用于指示单击按钮所在行的属性。如果需要知道哪个行引发了事件,请使用 CommandArgument 属性将行的索引传给事件处理方法。 |
下面的代码示例演示如何使用 RowCommand 事件在单击某行的“添加”按钮时将客户名称从 GridView 控件添加到 ListBox 控件。
<%@ Page language="C#" %>
<script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];
// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);
// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%">
<tr>
<td width="50%">
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
onrowcreated="CustomersGridView_RowCreated"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="City"
headertext="City"/>
</columns>
</asp:gridview>
</td>
<td valign="top" width="50%">
Customers: <br/>
<asp:listbox id="CustomersListBox"
runat="server"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>