DetailsView.ItemCreated イベント
アセンブリ: System.Web (system.web.dll 内)
構文
Public Event ItemCreated As EventHandler
public event EventHandler ItemCreated
public: event EventHandler^ ItemCreated { void add (EventHandler^ value); void remove (EventHandler^ value); }
/** @event */ public void add_ItemCreated (EventHandler value) /** @event */ public void remove_ItemCreated (EventHandler value)
ItemCreated イベントは、DetailsView コントロールでレコードが作成されるたびに発生します。これは、コントロールが最初に表示されたとき、またはユーザーが別のレコードに移動したときに発生することがあります。このイベントを使用して、このイベントが発生するたびにカスタム ルーチン (カスタム コンテンツの行への追加など) を実行するイベント ハンドラを提供できます。
HeaderRow、FooterRow、TopPagerRow、または BottomPagerRow の各プロパティを使用して、DetailsView コントロールのヘッダー、フッター、およびページ行にアクセスできます。いずれかのデータ行にアクセスするには、Rows コレクションを使用します。
ItemCreated イベントを使用して、カスタム ページ行を現在のページングの統計情報で更新する方法のコード例を次に示します。
<%@ Page language="VB" %> <script runat="server"> Protected Sub CustomerDetailView_DataBound(ByVal sender As Object, ByVal e As EventArgs) ' Get the pager row. Dim pagerRow As DetailsViewRow = CustomerDetailView.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 = CustomerDetailView.DataItemIndex + 1 Dim count As Integer = CustomerDetailView.DataItemCount pageNum.Text = page.ToString() totalNum.Text = count.ToString() End If End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3>DetailsView PagerTemplate Example</h3> <!-- Notice that the LinkButton controls in the pager --> <!-- template have their CommandName properties set. --> <!-- The DetailsView control automatically recognizes --> <!-- certain command names and performs the appropriate --> <!-- operation. In this example, the CommandName --> <!-- properties are set "Page" and the CommandArgument --> <!-- properties are set to "Next" and "Prev", which --> <!-- causes the DetailsView control to navigate to the --> <!-- next and previous record, respectively. --> <asp:detailsview id="CustomerDetailView" datasourceid="DetailsViewSource" autogeneraterows="true" allowpaging="true" runat="server" OnDataBound="CustomerDetailView_DataBound"> <headerstyle backcolor="Navy" forecolor="White"/> <pagerstyle VerticalAlign="Bottom" /> <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> </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="C#" %> <script runat="server"> protected void CustomerDetailView_DataBound(object sender, EventArgs e) { // Get the pager row. DetailsViewRow pagerRow = CustomerDetailView.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 = CustomerDetailView.DataItemIndex + 1; int count = CustomerDetailView.DataItemCount; pageNum.Text = page.ToString(); totalNum.Text = count.ToString(); } } </script> <html> <body> <form id="Form1" runat="server"> <h3>DetailsView PagerTemplate Example</h3> <!-- Notice that the LinkButton controls in the pager --> <!-- template have their CommandName properties set. --> <!-- The DetailsView control automatically recognizes --> <!-- certain command names and performs the appropriate --> <!-- operation. In this example, the CommandName --> <!-- properties are set "Page" and the CommandArgument --> <!-- properties are set to "Next" and "Prev", which --> <!-- causes the DetailsView control to navigate to the --> <!-- next and previous record, respectively. --> <asp:detailsview id="CustomerDetailView" datasourceid="DetailsViewSource" autogeneraterows="true" allowpaging="true" runat="server" OnDataBound="CustomerDetailView_DataBound"> <headerstyle backcolor="Navy" forecolor="White"/> <pagerstyle VerticalAlign="Bottom" /> <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> </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>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- DetailsView.ItemCreated イベントのページへのリンク