GridView.OnRowCreated(GridViewRowEventArgs) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 RowCreated 事件。
protected:
virtual void OnRowCreated(System::Web::UI::WebControls::GridViewRowEventArgs ^ e);
protected virtual void OnRowCreated (System.Web.UI.WebControls.GridViewRowEventArgs e);
abstract member OnRowCreated : System.Web.UI.WebControls.GridViewRowEventArgs -> unit
override this.OnRowCreated : System.Web.UI.WebControls.GridViewRowEventArgs -> unit
Protected Overridable Sub OnRowCreated (e As GridViewRowEventArgs)
参数
包含事件数据的 GridViewRowEventArgs。
示例
以下示例演示如何为 RowCreated 事件提供事件处理方法。 创建行时,其索引存储在 CommandArgument 新行中包含的控件的 LinkButton 属性中。 这使你可以在用户单击命令按钮时确定行的索引。
<%@ 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 ProductsGridView_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 = ProductsGridView.Rows[index];
// Create a new ListItem object for the product in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[1].Text);
// If the product is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ProductsListBox.Items.Contains(item))
{
ProductsListBox.Items.Add(item);
}
}
}
void ProductsGridView_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.FindControl("AddButton");
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
</script>
<html>
<head id="Head1" runat="server">
<title>GridView RowCreated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCreated Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:GridView ID="ProductsGridView"
DataSourceID="ProductsDataSource"
AllowPaging="true"
AutoGenerateColumns="false"
OnRowCommand="ProductsGridView_RowCommand"
OnRowCreated="ProductsGridView_RowCreated"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"
ID="AddButton"
CommandName="Add"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Product Name"/>
<asp:BoundField DataField="ProductNumber"
HeaderText="Product Number"/>
</Columns>
</asp:GridView>
</td>
<td style="vertical-align:top; width:50%">
Products: <br/>
<asp:listbox id="ProductsListBox"
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="ProductsDataSource"
selectcommand="Select [Name], [ProductNumber] From Production.Product"
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 ProductsGridView_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 = ProductsGridView.Rows(index)
' Create a new ListItem object for the product in the row.
Dim item As New ListItem()
item.Text = Server.HtmlDecode(row.Cells(1).Text)
' If the product is not already in the ListBox, add the ListItem
' object to the Items collection of the ListBox control.
If Not ProductsListBox.Items.Contains(item) Then
ProductsListBox.Items.Add(item)
End If
End If
End Sub
Sub ProductsGridView_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's button 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.FindControl("AddButton"), LinkButton)
' Set the LinkButton's CommandArgument property with the
' row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString()
End If
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>GridView RowCreated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCreated Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:GridView ID="ProductsGridView"
DataSourceID="ProductsDataSource"
AllowPaging="true"
AutoGenerateColumns="false"
OnRowCommand="ProductsGridView_RowCommand"
OnRowCreated="ProductsGridView_RowCreated"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"
ID="AddButton"
CommandName="Add"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Product Name"/>
<asp:BoundField DataField="ProductNumber"
HeaderText="Product Number"/>
</Columns>
</asp:GridView>
</td>
<td style="vertical-align:top; width:50%">
Products: <br/>
<asp:listbox id="ProductsListBox"
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="ProductsDataSource"
selectcommand="Select [Name], [ProductNumber] From Production.Product"
connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
GridView在呈现控件之前,必须为控件中的每一行创建 一个 GridViewRow 对象。 创建 RowCreated 控件中的每一行时, GridView 将引发 事件。 这使你可以提供执行自定义例程的事件处理方法,例如,每当发生此事件时,向行添加自定义内容。
引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件。
OnRowCreated 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。
继承者说明
在派生类中重写 OnRowCreated(GridViewRowEventArgs) 时,一定要调用基类的 OnRowCreated(GridViewRowEventArgs) 方法,以便已注册的委托对事件进行接收。