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


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

DataItem プロパティを使用して、データ項目のフィールドの値を確認する方法を次の例に示します。
<%@ page language="VB" %> <%@ import namespace="System.Data"%> <script runat="server"> ' To dynamically create a template for a FormView control, ' you must create a custom template class to represent ' the template. This template class represents the item ' template for a FormView control. Private NotInheritable Class EmployeeTemplate Implements ITemplate ' When implementing the ITemplate interface, you must ' implement the InstantiateIn method. The FormView ' control calls this method to create the template's ' content. Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn ' Create the child controls contained in the template. ' For this example, the item template displays the ' FirstName and LastName fields from the data source. ' To support data binding, create event handlers ' for the DataBinding event of each child child control. ' The event handler must bind the appropriate value ' to the control. Dim firstNameLabel As New Label() firstNameLabel.ID = "FirstNameLabel" AddHandler firstNameLabel.DataBinding, AddressOf FirstNameLabel_DataBinding Dim lineBreak As LiteralControl = New LiteralControl("<br/>") Dim lastNameLabel As New Label() lastNameLabel.ID = "LastNameLabel" AddHandler lastNameLabel.DataBinding, AddressOf LastNameLabel_DataBinding ' Add the controls to the Controls collection of the ' container control. container.Controls.Add(firstNameLabel) container.Controls.Add(lineBreak) container.Controls.Add(lastNameLabel) End Sub ' This event handler binds the value of the FirstName field ' to the FirstNameLabel Label control displayed in the template. Private Sub FirstNameLabel_DataBinding(ByVal sender As Object, ByVal e As EventArgs) ' Use the sender parameter to retrieve the Label control ' being bound to data. Dim firstNameLabelControl As Label = CType(sender, Label) ' Retrieve the value to bind to the Label control. First, ' use the NamingContainer property to retrieve the parent ' control of the Label control. In this example, the parent ' control is the FormView control. Dim formViewContainer As FormView = CType(firstNameLabelControl.NamingContainer, FormView) ' Get the data item bound to the FormView control. Dim rowView As DataRowView = CType(formViewContainer.DataItem, DataRowView) ' Use the data item to retrieve the value of the FirstName field ' Set the Text property of the Label control to this value. firstNameLabelControl.Text = rowView("FirstName").ToString() End Sub ' This event handler binds the value of the LastName field ' to the LastNameLabel Label control displayed in the template. Private Sub LastNameLabel_DataBinding(ByVal sender As Object, ByVal e As EventArgs) ' Use the sender parameter to retrieve the Label control ' being bound to data. Dim lastNameLabelControl As Label = CType(sender, Label) ' Retrieve the value to bind to the Label control. First, ' use the NamingContainer property to retrieve the parent ' control of the Label control. In this example, the parent ' control is the FormView control. Dim formViewContainer As FormView = CType(lastNameLabelControl.NamingContainer, FormView) ' Get the data item bound to the FormView control. Dim rowView As DataRowView = CType(formViewContainer.DataItem, DataRowView) ' Use the data item to retrieve the value of the LastName field ' Set the Text property of the Label control to this value. lastNameLabelControl.Text = rowView("LastName").ToString() End Sub End Class Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Create a new FormView object. Dim employeesFormView As New FormView() ' Set the FormView object's properties. employeesFormView.ID = "EmployeesFormView" employeesFormView.DataSourceID = "EmployeeSource" employeesFormView.AllowPaging = True employeesFormView.PagerSettings.Mode = PagerButtons.NextPrevious employeesFormView.HeaderText = "Employee Name" employeesFormView.RowStyle.BackColor = System.Drawing.Color.LightBlue employeesFormView.HeaderStyle.BackColor = System.Drawing.Color.Silver employeesFormView.PagerStyle.BackColor = System.Drawing.Color.Silver ' Create the dynamic template using the custom template class. employeesFormView.ItemTemplate = New EmployeeTemplate() ' Add the FormView object to the Controls collection ' of the PlaceHolder control. DetailsViewPlaceHolder.Controls.Add(employeesFormView) End Sub </script> <html> <body> <form runat="server"> <h3>FormView Constructor Example</h3> <!-- Use a PlaceHolder control as the container for the --> <!-- dynamically generated FormView control. --> <asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/> <!-- 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] From [Employees]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ page language="C#" %> <%@ import namespace="System.Data"%> <script runat="server"> // To dynamically create a template for a FormView control, // you must create a custom template class to represent // the template. This template class represents the item // template for a FormView control. private sealed class EmployeeTemplate : ITemplate { // When implementing the ITemplate interface, you must // implement the InstantiateIn method. The FormView // control calls this method to create the template's // content. void ITemplate.InstantiateIn(Control container) { // Create the child controls contained in the template. // For this example, the item template displays the // FirstName and LastName fields from the data source. // To support data binding, create event handlers // for the DataBinding event of each child child control. // The event handler must bind the appropriate value // to the control. Label firstNameLabel = new Label(); firstNameLabel.ID = "FirstNameLabel"; firstNameLabel.DataBinding += new EventHandler(FirstNameLabel_DataBinding); LiteralControl lineBreak = new LiteralControl("<br/>"); Label lastNameLabel = new Label(); lastNameLabel.ID = "LastNameLabel"; lastNameLabel.DataBinding += new EventHandler(LastNameLabel_DataBinding); // Add the controls to the Controls collection of the // container control. container.Controls.Add(firstNameLabel); container.Controls.Add(lineBreak); container.Controls.Add(lastNameLabel); } // This event handler binds the value of the FirstName field // to the FirstNameLabel Label control displayed in the template. private void FirstNameLabel_DataBinding(Object sender, EventArgs e) { // Use the sender parameter to retrieve the Label control // being bound to data. Label firstNameLabelControl = (Label)sender; // Retrieve the value to bind to the Label control. First, // use the NamingContainer property to retrieve the parent // control of the Label control. In this example, the parent // control is the FormView control. FormView formViewContainer = (FormView)firstNameLabelControl.NamingContainer; // Get the data item bound to the FormView control. DataRowView rowView = (DataRowView)formViewContainer.DataItem; // Use the data item to retrieve the value of the FirstName field // Set the Text property of the Label control to this value. firstNameLabelControl.Text = rowView["FirstName"].ToString(); } // This event handler binds the value of the LastName field // to the LastNameLabel Label control displayed in the template. private void LastNameLabel_DataBinding(Object sender, EventArgs e) { // Use the sender parameter to retrieve the Label control // being bound to data. Label lastNameLabelControl = (Label)sender; // Retrieve the value to bind to the Label control. First, // use the NamingContainer property to retrieve the parent // control of the Label control. In this example, the parent // control is the FormView control. FormView formViewContainer = (FormView)lastNameLabelControl.NamingContainer; // Get the data item bound to the FormView control. DataRowView rowView = (DataRowView)formViewContainer.DataItem; // Use the data item to retrieve the value of the LastName field. // Set the Text property of the Label control to this value. lastNameLabelControl.Text = rowView["LastName"].ToString(); } } void Page_Load(Object sender, EventArgs e) { // Create a new FormView object. FormView employeesFormView = new FormView(); // Set the FormView object's properties. employeesFormView.ID = "EmployeesFormView"; employeesFormView.DataSourceID = "EmployeeSource"; employeesFormView.AllowPaging = true; employeesFormView.PagerSettings.Mode = PagerButtons.NextPrevious; employeesFormView.HeaderText = "Employee Name"; employeesFormView.RowStyle.BackColor = System.Drawing.Color.LightBlue; employeesFormView.HeaderStyle.BackColor = System.Drawing.Color.Silver; employeesFormView.PagerStyle.BackColor = System.Drawing.Color.Silver; // Create the dynamic template using the custom template class. employeesFormView.ItemTemplate = new EmployeeTemplate(); // Add the FormView object to the Controls collection // of the PlaceHolder control. DetailsViewPlaceHolder.Controls.Add(employeesFormView); } </script> <html> <body> <form runat="server"> <h3>FormView Constructor Example</h3> <!-- Use a PlaceHolder control as the container for the --> <!-- dynamically generated FormView control. --> <asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/> <!-- 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] From [Employees]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からFormView.DataItem プロパティを検索する場合は、下記のリンクをクリックしてください。

- FormView.DataItem プロパティのページへのリンク