GridViewCommandEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 RowCommand 事件提供数据。
public ref class GridViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class GridViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type GridViewCommandEventArgs = class
inherit CommandEventArgs
Public Class GridViewCommandEventArgs
Inherits CommandEventArgs
- 继承
示例
以下示例演示如何使用 GridViewCommandEventArgs 传递给事件处理方法的对象来确定引发事件的按钮的命令名称。
<%@ 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 ContactsGridView_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 = ContactsGridView.Rows[index];
// Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
Server.HtmlDecode(row.Cells[3].Text);
// If the contact is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(item);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowCommand Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:gridview id="ContactsGridView"
datasourceid="ContactsSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="ContactsGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="ContactID"
headertext="Contact ID"/>
<asp:boundfield datafield="FirstName"
headertext="First Name"/>
<asp:boundfield datafield="LastName"
headertext="Last Name"/>
</columns>
</asp:gridview>
</td>
<td style="vertical-align:top; width:50%">
Contacts: <br/>
<asp:listbox id="ContactsListBox"
runat="server" Height="200px" Width="200px"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="ContactsSource"
selectcommand="Select [ContactID], [FirstName], [LastName] From Person.Contact"
connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>"
runat="server"/>
</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 ContactsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple buttons are used in a GridView control, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Add" Then
' Convert the row index stored in the CommandArgument
' property to an Integer.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button clicked
' by the user from the Rows collection.
Dim row As GridViewRow = ContactsGridView.Rows(index)
' Create a new ListItem object for the contact in the row.
Dim item As New ListItem()
item.Text = Server.HtmlDecode(row.Cells(2).Text) & " " & _
Server.HtmlDecode(row.Cells(3).Text)
' If the contact is not already in the ListBox, add the ListItem
' object to the Items collection of the ListBox control.
If Not ContactsListBox.Items.Contains(item) Then
ContactsListBox.Items.Add(item)
End If
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridView RowCommand Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCommand Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:gridview id="ContactsGridView"
datasourceid="ContactsSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="ContactsGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="ContactID"
headertext="Contact ID"/>
<asp:boundfield datafield="FirstName"
headertext="First Name"/>
<asp:boundfield datafield="LastName"
headertext="Last Name"/>
</columns>
</asp:gridview>
</td>
<td style="vertical-align:top; width:50%">
Contacts: <br/>
<asp:listbox id="ContactsListBox"
runat="server" Height="200px" Width="200px"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="ContactsSource"
selectcommand="Select [ContactID], [FirstName], [LastName] From Person.Contact"
connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
单击 RowCommand 控件中的按钮时, GridView 将引发 事件。 这允许你提供一个事件处理方法,该方法在发生此事件时执行自定义例程。
注意
当单击某些按钮时,控件 GridView 还会引发其他专用事件, (按钮的属性 CommandName
设置为“Delete”、“Update”和“Page”,例如) 。 使用这些按钮之一时,应考虑处理控件 (提供的一个专用事件,例如 RowDeleted 或 RowDeleting) 。
对象 GridViewCommandEventArgs 将传递给事件处理方法,这样就可以确定所单击按钮的命令名称和命令参数。 若要确定命令名称和命令参数,请分别使用 CommandName 和 CommandArgument 属性。 还可以使用 CommandSource 属性访问引发事件的按钮控件。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
有关 实例 GridViewCommandEventArgs的初始属性值列表, GridViewCommandEventArgs 请参阅 构造函数。
构造函数
GridViewCommandEventArgs(GridViewRow, Object, CommandEventArgs) |
使用指定的行、命令源和事件参数初始化 GridViewCommandEventArgs 类的新实例。 |
GridViewCommandEventArgs(Object, CommandEventArgs) |
使用指定的命令源和事件参数初始化 GridViewCommandEventArgs 类的新实例。 |
属性
CommandArgument |
获取命令的参数。 (继承自 CommandEventArgs) |
CommandName |
获取命令的名称。 (继承自 CommandEventArgs) |
CommandSource |
获取命令源。 |
Handled |
获取或设置指示控件是否已处理事件的值。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |