CompositeDataBoundControl.CreateChildControls 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立用來呈現複合資料繫結控制項的控制項階層架構。
多載
CreateChildControls() |
根據儲存在檢視狀態中的值,建立用來呈現複合資料繫結控制項的控制項階層架構。 |
CreateChildControls(IEnumerable, Boolean) |
在抽象類別中被覆寫時,根據指定之資料來源中的值,建立用來呈現複合資料繫結控制項的控制項階層架構。 |
CreateChildControls()
根據儲存在檢視狀態中的值,建立用來呈現複合資料繫結控制項的控制項階層架構。
protected public:
override void CreateChildControls();
protected internal override void CreateChildControls ();
override this.CreateChildControls : unit -> unit
Protected Friend Overrides Sub CreateChildControls ()
備註
方法 CreateChildControls() 是衍生自 類別的 CompositeDataBoundControl 協助程式方法,用來建立複合數據綁定控件的控件階層。 這個方法的多載會根據檢視狀態的值,而不是直接從數據源建立控件階層。
另請參閱
適用於
CreateChildControls(IEnumerable, Boolean)
在抽象類別中被覆寫時,根據指定之資料來源中的值,建立用來呈現複合資料繫結控制項的控制項階層架構。
protected:
abstract int CreateChildControls(System::Collections::IEnumerable ^ dataSource, bool dataBinding);
protected abstract int CreateChildControls (System.Collections.IEnumerable dataSource, bool dataBinding);
override this.CreateChildControls : System.Collections.IEnumerable * bool -> int
Protected MustOverride Function CreateChildControls (dataSource As IEnumerable, dataBinding As Boolean) As Integer
參數
- dataSource
- IEnumerable
IEnumerable,包含要繫結至控制項的值。
- dataBinding
- Boolean
true
表示會在資料繫結 (Data Binding) 期間呼叫 CreateChildControls(IEnumerable, Boolean),否則為 false
。
傳回
由 CreateChildControls(IEnumerable, Boolean) 所建立之項目的數目。
範例
下列程式代碼範例示範如何覆寫 CreateChildControls(IEnumerable, Boolean) 自定義控件中的方法,以建立控件階層。
using System;
using System.Collections;
using System.Data.Common;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Samples.AspNet.CS
{
public class SimpleSpreadsheetControl : CompositeDataBoundControl
{
protected Table table = new Table();
public virtual TableRowCollection Rows
{
get
{
return table.Rows;
}
}
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int count = 0;
// If dataSource is not null, iterate through it and
// extract each element from it as a row, then
// create a SimpleSpreadsheetRow and add it to the
// rows collection.
if (dataSource != null)
{
SimpleSpreadsheetRow row;
IEnumerator e = dataSource.GetEnumerator();
while (e.MoveNext())
{
object datarow = e.Current;
row = new SimpleSpreadsheetRow(count, datarow);
this.Rows.Add(row);
++count;
}
Controls.Add(table);
}
return count;
}
}
//
//
public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
{
private object data;
private int _itemIndex;
public SimpleSpreadsheetRow(int itemIndex, object o)
{
data = o;
_itemIndex = itemIndex;
}
public virtual object Data
{
get
{
return data;
}
}
object IDataItemContainer.DataItem
{
get
{
return Data;
}
}
int IDataItemContainer.DataItemIndex
{
get
{
return _itemIndex;
}
}
int IDataItemContainer.DisplayIndex
{
get
{
return _itemIndex;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
if (Data != null)
{
if (Data is System.Data.Common.DbDataRecord)
{
DbDataRecord temp = (DbDataRecord)Data;
for (int i = 0; i < temp.FieldCount; ++i)
{
writer.Write("<TD>");
writer.Write(temp.GetValue(i).ToString());
writer.Write("</TD>");
}
}
else
{
writer.Write("<TD>" + Data.ToString() + "</TD>");
}
}
else
{
writer.Write("<TD>This is a test</TD>");
}
}
}
}
Imports System.Collections
Imports System.Data.Common
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB
Public Class SimpleSpreadsheetControl
Inherits CompositeDataBoundControl
Protected table As New Table()
Public Overridable ReadOnly Property Rows() As TableRowCollection
Get
Return table.Rows
End Get
End Property
Protected Overrides Function CreateChildControls(ByVal dataSource As IEnumerable, ByVal dataBinding As Boolean) As Integer
Dim count As Integer = 0
' If dataSource is not Nothing, iterate through it and
' extract each element from it as a row, then
' create a SimpleSpreadsheetRow and add it to the
' rows collection.
If Not (dataSource Is Nothing) Then
Dim row As SimpleSpreadsheetRow
Dim e As IEnumerator = dataSource.GetEnumerator()
While e.MoveNext()
Dim datarow As Object = e.Current
row = New SimpleSpreadsheetRow(count, datarow)
Me.Rows.Add(row)
count += 1
End While
Controls.Add(table)
End If
Return count
End Function 'CreateChildControls
End Class
Public Class SimpleSpreadsheetRow
Inherits TableRow
Implements IDataItemContainer
Private dataObj As Object
Private _itemIndex As Integer
Public Sub New(ByVal itemIndex As Integer, ByVal o As Object)
dataObj = o
_itemIndex = itemIndex
End Sub
Public Overridable ReadOnly Property Data() As Object
Get
Return dataObj
End Get
End Property
ReadOnly Property DataItem() As Object Implements IDataItemContainer.DataItem
Get
Return Data
End Get
End Property
ReadOnly Property DataItemIndex() As Integer Implements IDataItemContainer.DataItemIndex
Get
Return _itemIndex
End Get
End Property
ReadOnly Property DisplayIndex() As Integer Implements IDataItemContainer.DisplayIndex
Get
Return _itemIndex
End Get
End Property
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
If Not (Data Is Nothing) Then
If TypeOf Data Is System.Data.Common.DbDataRecord Then
Dim temp As DbDataRecord = CType(Data, DbDataRecord)
Dim i As Integer
While i < temp.FieldCount
writer.Write("<TD>")
writer.Write(temp.GetValue(i).ToString())
writer.Write("</TD>")
i += 1
End While
Else
writer.Write(("<TD>" + Data.ToString() + "</TD>"))
End If
Else
writer.Write("<TD>This is a test</TD>")
End If
End Sub
End Class
End Namespace
備註
方法 CreateChildControls() 是衍生自 類別的 CompositeDataBoundControl 協助程式方法,用來建立複合數據綁定控件的控件階層。 擴充 CompositeDataBoundControl 類別時,您必須覆寫 CreateChildControls() 方法來建立您自己的控件階層。 如需建立複合控件的詳細資訊,請參閱 開發自定義 ASP.NET 伺服器控件。