DataBoundControl クラスとは? わかりやすく解説

DataBoundControl クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

データを一覧または表形式表示するすべての ASP.NET Version 2.0 データ バインド コントロール対し基本クラスとして機能します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public MustInherit Class
 DataBoundControl
    Inherits BaseDataBoundControl
Dim instance As DataBoundControl
public abstract class DataBoundControl : BaseDataBoundControl
public ref class DataBoundControl abstract
 : public BaseDataBoundControl
public abstract class DataBoundControl extends
 BaseDataBoundControl
public abstract class DataBoundControl extends
 BaseDataBoundControl
解説解説

DataBoundControl クラスは、ASP.NET コントロール使用される基本クラスです。このコントロールは、ASP.NET データ ソース コントロールから表形式または一覧形式データ取得しコントロールユーザー インターフェイス (UI) 要素をそのデータバインドして表示します。GridView、DetailsView、FormView などの複合データ バインド コントロール、BulletedList、CheckBoxList などの一覧形式データ バインド コントロール、AdRotator などのその他のコントロールは、DataBoundControl から派生します。

ページ開発者は、DataBoundControl クラス直接使用せずに、このクラスから派生するコントロール使用します

コントロール開発者はこのクラス拡張して、IDataSource インターフェイス実装するクラス、および DataSourceControl クラスと DataSourceView クラスから派生するクラス機能するデータ バインド コントロール作成しますDataBoundControl クラスから派生クラス作成する場合、PerformDataBinding メソッドオーバーライドして、コントロールUI 要素を GetData メソッド取得したデータバインドます。ほとんどの場合派生クラスオーバーライドするメソッドは、PerformDataBinding メソッドだけです。

ASP.NET 2.0 データ バインド コントロール場合、PerformSelect メソッドDataBind メソッド等価で、実行時データバインドするために呼び出されます。PerformSelect メソッドは、GetData メソッドPerformDataBinding メソッド呼び出します。

使用例使用例

DataBoundControl クラスから派生クラス作成してカスタムデータ バインド コントロール作成する方法次のコード例示しますTextBoxSet コントロールは、関連するデータ ソース コントロールから取得したデータ項目に対して TextBox コントロール作成し実行時データ項目の値にバインドます。Render メソッド現在の実装では、TextBox コントロール順序なしの一覧として描画ます。

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.Controls.VB

<AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal), _
    AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class TextBoxSet
    Inherits DataBoundControl

    Private alBoxSet As IList

    Public ReadOnly Property
 BoxSet() As IList
        Get
            If alBoxSet Is Nothing
 Then
                alBoxSet = New ArrayList()
            End If
            Return alBoxSet
        End Get
    End Property

    Public Property DataTextField() As
 String
        Get
            Dim o As Object
 = ViewState("DataTextField")
            If o Is Nothing
 Then
                Return String.Empty
            Else
                Return CStr(o)
            End If
        End Get
        Set(ByVal value As
 String)
            ViewState("DataTextField") = value
            If (Initialized) Then
                OnDataPropertyChanged()
            End If
        End Set
    End Property

    Protected Overrides Sub
 PerformSelect()

        ' Call OnDataBinding here if bound to a data source using the
 
        ' DataSource property (instead of a DataSourceID) because the
 
        ' data-binding statement is evaluated before the call to GetData.
        If Not IsBoundUsingDataSourceID Then
            OnDataBinding(EventArgs.Empty)
        End If

        ' The GetData method retrieves the DataSourceView object from
 the 
        ' IDataSource associated with the data-bound control.      
      
        GetData().Select(CreateDataSourceSelectArguments(), _
            AddressOf OnDataSourceViewSelectCallback)

        ' The PerformDataBinding method has completed.
        RequiresDataBinding = False
        MarkAsDataBound()

        ' Raise the DataBound event.
            OnDataBound(EventArgs.Empty)

    End Sub 'PerformSelect

    Private Sub OnDataSourceViewSelectCallback(ByVal
 retrievedData As IEnumerable)

        ' Call OnDataBinding only if it has not already
        ' been called in the PerformSelect method.
        If IsBoundUsingDataSourceID Then
            OnDataBinding(EventArgs.Empty)
        End If
        ' The PerformDataBinding method binds the data in the retrievedData
 
        ' collection to elements of the data-bound control.
        PerformDataBinding(retrievedData)

    End Sub 'OnDataSourceViewSelectCallback

    Protected Overrides Sub
 PerformDataBinding(ByVal retrievedData As
 IEnumerable)
        MyBase.PerformDataBinding(retrievedData)

        ' If the data is retrieved from an IDataSource as an IEnumerable
 
        ' collection, attempt to bind its values to a set of TextBox
 controls.
        If Not (retrievedData Is
 Nothing) Then

            Dim dataItem As Object
            For Each dataItem In
 retrievedData

                Dim box As New
 TextBox()

                ' The dataItem is not just a string, but potentially
                ' a System.Data.DataRowView or some other container.
 
                ' If DataTextField is set, use it to determine which
 
                ' field to render. Otherwise, use the first field. 
                   
                If DataTextField.Length > 0 Then
                    box.Text = DataBinder.GetPropertyValue( _
                    dataItem, DataTextField, Nothing)
                Else
                    Dim props As PropertyDescriptorCollection
 = _
                        TypeDescriptor.GetProperties(dataItem)

                    ' Set the "default" value of the TextBox.
                    box.Text = String.Empty

                    ' Set the true data-bound value of the TextBox,
                    ' if possible.
                    If props.Count >= 1 Then
                        If props(0).GetValue(dataItem) IsNot Nothing
 Then
                            box.Text = props(0).GetValue(dataItem).ToString()
                        End If
                    End If
                End If

                BoxSet.Add(box)
            Next dataItem
        End If

    End Sub 'PerformDataBinding

    Protected Overrides Sub
 Render(ByVal writer As HtmlTextWriter)

        ' Render nothing if the control is empty.            
        If BoxSet.Count <= 0 Then
            Return
        End If

        ' Make sure the control is declared in a form tag with runat=server.
        If Not (Page Is
 Nothing) Then
            Page.VerifyRenderingInServerForm(Me)
        End If

        ' For this example, render the BoxSet as 
        ' an unordered list of TextBox controls.            
        writer.RenderBeginTag(HtmlTextWriterTag.Ul)

        Dim item As Object
        For Each item In
 BoxSet

            Dim box As TextBox = CType(item,
 TextBox)

            ' Write each element as 
            ' <li><input type="text" value="string"><input/></li>
            writer.WriteBeginTag("li")
            writer.Write(">")
            writer.WriteBeginTag("input")
            writer.WriteAttribute("type", "text")
            writer.WriteAttribute("value", box.Text)
            writer.Write(">")
            writer.WriteEndTag("input")
            writer.WriteEndTag("li")
        Next item

        writer.RenderEndTag()

    End Sub 'Render
End Namespace
using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.Controls.CS {

    [AspNetHostingPermission(SecurityAction.Demand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    public class TextBoxSet : DataBoundControl
 {

        private IList alBoxSet;
        public IList BoxSet {
            get {
                if (null == alBoxSet) {
                     alBoxSet = new ArrayList();
                }
                return alBoxSet;                
            }                    
        }
        public string DataTextField {
            get {
                object o = ViewState["DataTextField"];
                return((o == null) ? string.Empty
 : (string)o);
            }
            set {
                ViewState["DataTextField"] = value;
                if (Initialized) {
                    OnDataPropertyChanged();
                }
            }
        }
        protected override void PerformSelect()
 {            

           // Call OnDataBinding here if bound to a data source using
 the
           // DataSource property (instead of a DataSourceID), because
 the
           // databinding statement is evaluated before the call to
 GetData.       
            if (! IsBoundUsingDataSourceID) {
                OnDataBinding(EventArgs.Empty);
            }            
            
            // The GetData method retrieves the DataSourceView object
 from  
            // the IDataSource associated with the data-bound control.
            
            GetData().Select(CreateDataSourceSelectArguments(), 
                OnDataSourceViewSelectCallback);
            
            // The PerformDataBinding method has completed.
            RequiresDataBinding = false;
            MarkAsDataBound();
            
            // Raise the DataBound event.
            OnDataBound(EventArgs.Empty);
        }
        private void OnDataSourceViewSelectCallback(IEnumerable
 retrievedData) {
        
           // Call OnDataBinding only if it has not already been 
           // called in the PerformSelect method.
            if (IsBoundUsingDataSourceID) {
                OnDataBinding(EventArgs.Empty);
            }
            // The PerformDataBinding method binds the data in the 
 
            // retrievedData collection to elements of the data-bound
 control.
            PerformDataBinding(retrievedData);                                  
  
        }
        protected override void PerformDataBinding(IEnumerable
 retrievedData) {
            base.PerformDataBinding(retrievedData);

            // If the data is retrieved from an IDataSource as an 
            // IEnumerable collection, attempt to bind its values to
 a 
            // set of TextBox controls.
            if (retrievedData != null) {

                foreach (object dataItem in
 retrievedData) {
                    
                    TextBox box = new TextBox();
                    
                    // The dataItem is not just a string, but potentially
                    // a System.Data.DataRowView or some other container.
 
                    // If DataTextField is set, use it to determine
 which 
                    // field to render. Otherwise, use the first field.
                    
                    if (DataTextField.Length > 0) {
                        box.Text = DataBinder.GetPropertyValue(dataItem, 
                            DataTextField, null);
                    }
                    else {
                        PropertyDescriptorCollection props = 
                            TypeDescriptor.GetProperties(dataItem);

                        // Set the "default" value of the
 TextBox.
                        box.Text = String.Empty;
                        
                        // Set the true data-bound value of the TextBox
,
                        // if possible.
                        if (props.Count >= 1) {           
             
                            if (null != props[0].GetValue(dataItem))
 {
                                box.Text = props[0].GetValue(dataItem).ToString();
                            }
                        }
                    }                                        
                    
                    BoxSet.Add(box);
                }
            }
        }
        protected override void Render(HtmlTextWriter
 writer) {

            // Render nothing if the control is empty.            
            if (BoxSet.Count <= 0) {
                return;
            }

            // Make sure the control is declared in a form tag 
            // with runat=server.
            if (Page != null) {
                Page.VerifyRenderingInServerForm(this);
            }
            
            // For this example, render the BoxSet as 
            // an unordered list of TextBox controls.            
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            foreach (object item in BoxSet)
 {
                 
                TextBox box = (TextBox) item;
                
                // Write each element as 
                // <li><input type="text" value="string"><input/></li>
                
                writer.WriteBeginTag("li");
                writer.Write(">");
                writer.WriteBeginTag("input");
                writer.WriteAttribute("type", "text");
                writer.WriteAttribute("value", box.Text);
                writer.Write(">");
                writer.WriteEndTag("input");
                writer.WriteEndTag("li");
            }            

            writer.RenderEndTag();                       
        }
    }
}

前の例で定義した TextBoxSet コントロール使用して、AccessDataSource コントロールバインドする方法次のコード例示します

<%@Page language="VB" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.VB"
 
    Assembly="Samples.AspNet.Controls.VB"
 %>

<html>
  <head>
    <title>TextBoxSet Data-Bound Control - VB Example</title>
  </head>

  <body>
    <form id="Form1" method="post"
 runat="server">

        <aspSample:textboxset
          id="TextBoxSet1"
          runat="server"
          datasourceid="AccessDataSource1" />

        <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datafile="Northwind.mdb"
          selectcommand="SELECT lastname FROM Employees"
 />
          
    </form>
  </body>
</html>
<%@Page language="c#" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.Controls.CS"
 
    Assembly="Samples.AspNet.Controls.CS" %>

<html>
  <head>
    <title>TextBoxSet Data-Bound Control  - C# Example</title>
  </head>

  <body>
    <form id="Form1" method="post" runat="server">

        <aspSample:textboxset
          id="TextBoxSet1"
          runat="server"
          datasourceid="AccessDataSource1" />

        <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datafile="Northwind.mdb"
          selectcommand="SELECT lastname FROM Employees" />
          
    </form>
  </body>
</html>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.BaseDataBoundControl
        System.Web.UI.WebControls.DataBoundControl
           System.Web.UI.WebControls.AdRotator
           System.Web.UI.WebControls.CompositeDataBoundControl
           System.Web.UI.WebControls.ListControl
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataBoundControl メンバ
System.Web.UI.WebControls 名前空間
HierarchicalDataBoundControl
CompositeDataBoundControl クラス



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataBoundControl クラス」の関連用語

DataBoundControl クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataBoundControl クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS