FormView.ItemCreated 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 FormView 控件中创建了所有行后发生。
public:
event EventHandler ^ ItemCreated;
public event EventHandler ItemCreated;
member this.ItemCreated : EventHandler
Public Custom Event ItemCreated As EventHandler
事件类型
示例
以下示例演示如何使用 ItemCreated 事件更新页包含的控件的文本 Label 。
<%@ 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 EmployeeFormView_ItemCreated(Object sender, EventArgs e)
{
// Get the pager row.
FormViewRow pagerRow = EmployeeFormView.BottomPagerRow;
// Get the Label controls that display the current page information
// from the pager row.
Label pageNum = (Label)pagerRow.Cells[0].FindControl("PageNumberLabel");
Label totalNum = (Label)pagerRow.Cells[0].FindControl("TotalPagesLabel");
if ((pageNum != null) && (totalNum != null))
{
// Update the Label controls with the current page values.
int page = EmployeeFormView.PageIndex + 1;
int count = EmployeeFormView.PageCount;
pageNum.Text = page.ToString();
totalNum.Text = count.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView PagerTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView PagerTemplate Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
onitemcreated="EmployeeFormView_ItemCreated"
runat="server">
<itemtemplate>
<table>
<tr>
<td>
<asp:image id="EmployeeImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td>
<h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>
<%# Eval("Title") %>
</td>
</tr>
</table>
</itemtemplate>
<pagertemplate>
<table width="100%">
<tr>
<td>
<asp:linkbutton id="PreviousButton"
text="<"
commandname="Page"
commandargument="Prev"
runat="Server"/>
<asp:linkbutton id="NextButton"
text=">"
commandname="Page"
commandargument="Next"
runat="Server"/>
</td>
<td align="right">
Page <asp:label id="PageNumberLabel" runat="server"/>
of <asp:label id="TotalPagesLabel" runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
<pagersettings position="Bottom"
mode="NextPrevious"/>
</asp:formview>
<!-- 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="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
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 EmployeeFormView_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
' Get the pager row.
Dim pagerRow As FormViewRow = EmployeeFormView.BottomPagerRow
' Get the Label controls that display the current page information
' from the pager row.
Dim pageNum As Label = CType(pagerRow.Cells(0).FindControl("PageNumberLabel"), Label)
Dim totalNum As Label = CType(pagerRow.Cells(0).FindControl("TotalPagesLabel"), Label)
If pageNum IsNot Nothing And totalNum IsNot Nothing Then
' Update the Label controls with the current page values.
Dim page As Integer = EmployeeFormView.PageIndex + 1
Dim count As Integer = EmployeeFormView.PageCount
pageNum.Text = page.ToString()
totalNum.Text = count.ToString()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView PagerTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView PagerTemplate Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
onitemcreated="EmployeeFormView_ItemCreated"
runat="server">
<itemtemplate>
<table>
<tr>
<td>
<asp:image id="EmployeeImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
</td>
<td>
<h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>
<%# Eval("Title") %>
</td>
</tr>
</table>
</itemtemplate>
<pagertemplate>
<table width="100%">
<tr>
<td>
<asp:linkbutton id="PreviousButton"
text="<"
commandname="Page"
commandargument="Prev"
runat="Server"/>
<asp:linkbutton id="NextButton"
text=">"
commandname="Page"
commandargument="Next"
runat="Server"/>
</td>
<td align="right">
Page <asp:label id="PageNumberLabel" runat="server"/>
of <asp:label id="TotalPagesLabel" runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
<pagersettings position="Bottom"
mode="NextPrevious"/>
</asp:formview>
<!-- 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="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
在 ItemCreated 控件中创建 FormView 所有行后,将引发 该事件。 首次呈现控件或用户导航到另一条记录时,可能会发生这种情况。 可以使用此事件提供一个事件处理方法,该方法在发生此事件时执行自定义例程,例如添加到或修改行的内容。
备注
该 ItemCreated 事件在控件绑定到数据之前 FormView 发生。 若要修改绑定字段的值,请使用 DataBound 事件。
可以通过分别使用 HeaderRow、TopPagerRowFooterRow、 或 BottomPagerRow 属性访问控件中的FormView页眉、页脚和页脚行。 若要访问数据行,请使用 Row 属性。
有关如何处理事件的详细信息,请参阅 处理和引发事件。