DetailsView.ItemCreated 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 DetailsView 控件中创建记录时发生。
public:
event EventHandler ^ ItemCreated;
public event EventHandler ItemCreated;
member this.ItemCreated : EventHandler
Public Custom Event ItemCreated As EventHandler
事件类型
示例
下面的代码示例演示如何使用 ItemCreated 事件来更新具有当前分页统计信息的自定义页导航行。
<%@ 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 CustomerDetailView_ItemCreated(Object sender, EventArgs e)
{
// Get the header row.
DetailsViewRow headerRow = CustomerDetailView.HeaderRow;
// Get the Label control that displays the current page information
// from the header row.
Label pageNum = (Label)headerRow.FindControl("PageNumberLabel");
if(pageNum != null)
{
// Update the Label control with the current page number.
int page = CustomerDetailView.DataItemIndex + 1;
pageNum.Text = "Page " + page.ToString ();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView HeaderTemplate Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView HeaderTemplate Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
allowpaging="true"
onitemcreated="CustomerDetailView_ItemCreated"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
<pagersettings Mode="NextPreviousFirstLast"/>
<headertemplate>
<table width="100%">
<tr>
<td align="left">
<asp:Image id="LogoImage"
imageurl="~\images\Logo.jpg"
AlternateText="Our logo"
runat="server"/>
</td>
<td align="right" valign="bottom">
<asp:Label id="PageNumberLabel"
font-size="9"
forecolor="DodgerBlue"
runat="server"/>
</td>
</tr>
</table>
</headertemplate>
</asp:detailsview>
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</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 CustomerDetailView_ItemCreated(ByVal sender As Object, _
ByVal e As EventArgs)
' Get the header row.
Dim headerRow As DetailsViewRow = CustomerDetailView.HeaderRow
' Get the Label control that displays the current page information
' from the header row.
Dim pageNum As Label = _
CType(headerRow.FindControl("PageNumberLabel"), Label)
If Not pageNum Is Nothing Then
' Update the Label control with the current page number.
Dim page As Integer = CustomerDetailView.DataItemIndex + 1
pageNum.Text = "Page " & page.ToString()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView HeaderTemplate Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView HeaderTemplate Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
allowpaging="true"
onitemcreated="CustomerDetailView_ItemCreated"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
<pagersettings Mode="NextPreviousFirstLast"/>
<headertemplate>
<table width="100%">
<tr>
<td align="left">
<asp:Image id="LogoImage"
imageurl="~\images\Logo.jpg"
AlternateText="Our logo"
runat="server"/>
</td>
<td align="right" valign="bottom">
<asp:Label id="PageNumberLabel"
font-size="9"
forecolor="DodgerBlue"
runat="server"/>
</td>
</tr>
</table>
</headertemplate>
</asp:detailsview>
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
注解
ItemCreated每次在 控件中创建DetailsView记录时都会引发 事件。 首次呈现控件或用户导航到另一条记录时,可能会发生这种情况。 可以使用此事件提供一个事件处理程序,该事件处理程序执行自定义例程,例如,每当发生此事件时,向行添加自定义内容。
可以通过分别使用 HeaderRow、TopPagerRowFooterRow、 或 BottomPagerRow 属性访问 控件中的DetailsView页眉、页脚和页脚行。 若要访问其中一个数据行,请使用 Rows 集合。
有关如何处理事件的详细信息,请参阅 处理和引发事件。