GridViewCommandEventHandler 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示处理 RowCommand 控件的 GridView 事件的方法。
public delegate void GridViewCommandEventHandler(System::Object ^ sender, GridViewCommandEventArgs ^ e);
public delegate void GridViewCommandEventHandler(object sender, GridViewCommandEventArgs e);
type GridViewCommandEventHandler = delegate of obj * GridViewCommandEventArgs -> unit
Public Delegate Sub GridViewCommandEventHandler(sender As Object, e As GridViewCommandEventArgs)
参数
- sender
- Object
事件源。
包含事件数据的 GridViewCommandEventArgs 对象。
示例
下面的示例演示如何以编程方式向 控件的 GridView 事件添加GridViewCommandEventHandler委托RowCommand。
<%@ 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 Page_Load(Object sender, EventArgs e)
{
// Create a new GridView object.
GridView customersGridView = new GridView();
// Set the GridView object's properties.
customersGridView.ID = "CustomersGridView";
customersGridView.DataSourceID = "CustomersSource";
customersGridView.AutoGenerateColumns = false;
// Dynamically create the columns for the GridView control.
ButtonField addColumn = new ButtonField();
addColumn.CommandName = "Add";
addColumn.Text = "Add";
addColumn.ButtonType = ButtonType.Link;
BoundField companyNameColumn = new BoundField();
companyNameColumn.DataField = "CompanyName";
companyNameColumn.HeaderText = "Company Name";
BoundField cityColumn = new BoundField();
cityColumn.DataField = "City";
cityColumn.HeaderText = "City";
// Add the columns to the Columns collection
// of the GridView control.
customersGridView.Columns.Add(addColumn);
customersGridView.Columns.Add(companyNameColumn);
customersGridView.Columns.Add(cityColumn);
// Programmatically register the event handling methods.
customersGridView.RowCommand += new GridViewCommandEventHandler(this.CustomersGridView_RowCommand);
customersGridView.RowCreated += new GridViewRowEventHandler(this.CustomersGridView_RowCreated);
// Add the GridView object to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView);
}
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField columns are used, 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. Use the
// CommandSource property to access the GridView control.
GridView customersGridView = (GridView)e.CommandSource;
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[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text);
// If the author is not already in the ListBox, add the ListItem
// object to the Items collection of a 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 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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewCommandEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewCommandEventHandler Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:placeholder id="GridViewPlaceHolder"
runat="Server"/>
</td>
<td style="vertical-align: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>
<%@ 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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create a new GridView object.
Dim customersGridView As New GridView()
' Set the GridView object's properties.
customersGridView.ID = "CustomersGridView"
customersGridView.DataSourceID = "CustomersSource"
customersGridView.AutoGenerateColumns = False
' Dynamically create the columns for the GridView control.
Dim addColumn As New ButtonField()
addColumn.CommandName = "Add"
addColumn.Text = "Add"
addColumn.ButtonType = ButtonType.Link
Dim companyNameColumn As New BoundField()
companyNameColumn.DataField = "CompanyName"
companyNameColumn.HeaderText = "Company Name"
Dim cityColumn As New BoundField()
cityColumn.DataField = "City"
cityColumn.HeaderText = "City"
' Add the columns to the Columns collection
' of the GridView control.
customersGridView.Columns.Add(addColumn)
customersGridView.Columns.Add(companyNameColumn)
customersGridView.Columns.Add(cityColumn)
' Programmatically register the event handling methods.
AddHandler customersGridView.RowCommand, AddressOf CustomersGridView_RowCommand
AddHandler customersGridView.RowCreated, AddressOf CustomersGridView_RowCreated
' Add the GridView object to the Controls collection
' of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView)
End Sub
Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple ButtonField columns are used, 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. Use the
' CommandSource property to access the GridView control.
Dim customersGridView As GridView = CType(e.CommandSource, GridView)
Dim row As GridViewRow = customersGridView.Rows(index)
' Create a new ListItem object for the customer in the row.
Dim item As New ListItem()
item.Text = Server.HtmlDecode(row.Cells(1).Text) + " " + Server.HtmlDecode(row.Cells(2).Text)
' If the author is not already in the ListBox, add the ListItem
' object to the Items collection of a ListBox control.
If Not CustomersListBox.Items.Contains(item) Then
CustomersListBox.Items.Add(item)
End If
End If
End Sub
Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' The GridViewCommandEventArgs class does not contain a
' property that indicates which row's command button was
' clicked. To identify which row was clicked, use the button's
' CommandArgument property by setting it to the row's index.
If e.Row.RowType = DataControlRowType.DataRow Then
' Retrieve the LinkButton control from the first column.
Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
' Set the LinkButton's CommandArgument property with the
' row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewCommandEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewCommandEventHandler Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:placeholder id="GridViewPlaceHolder"
runat="Server"/>
</td>
<td style="vertical-align: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>
以下示例演示如何以声明方式向 控件的 GridView 事件添加GridViewCommandEventHandler委托RowCommand。
<%@ 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 按钮时,将引发 事件。 这允许你提供一个事件处理方法,该方法在发生此事件时执行自定义例程。
注意
当某些按钮在属性设置为“Delete”、“Update”和“Page”(例如,) ) (按钮CommandName
单击时,控件GridView也会引发其他专用事件。 使用其中一个按钮时,应考虑处理控件 (提供的专用事件之一,例如 RowDeleted 或 RowDeleting) 。
创建 GridViewCommandEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |