DetailsView.DataItem プロパティ
アセンブリ: System.Web (system.web.dll 内)


DataItem プロパティを使用して、DetailsView コントロールにバインドされたデータ項目にアクセスします。データ項目は、現在のレコードのフィールド値に直接アクセスする場合によく使用されます。このプロパティから返される Object の型は、データ ソースによって異なります。たとえば、SqlDataSource コントロールが DetailsView コントロールにバインドされている場合は、DataRowView オブジェクトが返されます。
![]() |
---|
このプロパティは、データ バインディングが発生した後にしか使用できません。まだデータ バインディングが発生していない場合、このプロパティは null 参照 (Visual Basic では Nothing) を返します。また、このプロパティは、DetailsView コントロールが挿入モードの場合にも適用できず、null 参照 (Visual Basic では Nothing) を返します。 |

DataItem プロパティを使用して、データ項目のフィールドの値を確認する方法のコード例を次に示します。
<%@ Page language="VB" %> <%@ import namespace="System.Data"%> <script runat="server"> Protected Sub CustomerDetailView_DataBound( _ ByVal sender As Object, ByVal e As EventArgs) Dim rowView As DataRowView = _ CType(CustomerDetailView.DataItem, DataRowView) If rowView.Row(0).ToString() = "SpecialID" Then CustomerDetailView.FieldHeaderStyle.BackColor = _ System.Drawing.Color.Red End If End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3>DetailsView ModeChanging Example</h3> <asp:detailsview id="CustomerDetailView" datasourceid="DetailsViewSource" datakeynames="CustomerID" autogeneraterows="true" autogenerateeditbutton="true" OnDataBound="CustomerDetailView_DataBound" allowpaging="true" runat="server"> <fieldheaderstyle backcolor="Navy" forecolor="White"/> </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#" %> <%@ import namespace="System.Data"%> <script runat="server"> protected void CustomerDetailView_DataBound(object sender, EventArgs e) { DataRowView rowView = (DataRowView)CustomerDetailView.DataItem; if (rowView.Row[0].ToString() == "SpecialID") { CustomerDetailView.FieldHeaderStyle.BackColor = System.Drawing.Color.Red; } } </script> <html> <body> <form id="Form1" runat="server"> <h3>DetailsView ModeChanging Example</h3> <asp:detailsview id="CustomerDetailView" datasourceid="DetailsViewSource" datakeynames="CustomerID" autogeneraterows="true" autogenerateeditbutton="true" OnDataBound="CustomerDetailView_DataBound" allowpaging="true" runat="server"> <fieldheaderstyle backcolor="Navy" forecolor="White"/> </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.DataItem プロパティのページへのリンク