DataControlField.InitializeCell 方法

定義

將文字或控制項加入至儲存格的控制項集合。

public:
 virtual void InitializeCell(System::Web::UI::WebControls::DataControlFieldCell ^ cell, System::Web::UI::WebControls::DataControlCellType cellType, System::Web::UI::WebControls::DataControlRowState rowState, int rowIndex);
public virtual void InitializeCell (System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex);
abstract member InitializeCell : System.Web.UI.WebControls.DataControlFieldCell * System.Web.UI.WebControls.DataControlCellType * System.Web.UI.WebControls.DataControlRowState * int -> unit
override this.InitializeCell : System.Web.UI.WebControls.DataControlFieldCell * System.Web.UI.WebControls.DataControlCellType * System.Web.UI.WebControls.DataControlRowState * int -> unit
Public Overridable Sub InitializeCell (cell As DataControlFieldCell, cellType As DataControlCellType, rowState As DataControlRowState, rowIndex As Integer)

參數

cell
DataControlFieldCell

DataControlFieldCell,其中包含 DataControlField 的文字或控制項。

cellType
DataControlCellType

其中一個 DataControlCellType 值。

rowState
DataControlRowState

其中一個 DataControlRowState 值,指定包含 DataControlFieldCell 之資料列的狀態。

rowIndex
Int32

資料列的索引,該資料列中包含 DataControlFieldCell

範例

下列程式碼範例示範如何為衍生自 類別的 DataControlField 控制項實 InitializeCell 作 方法。 類別 RadioButtonField 會呈現 控制項中 GridView 每個資料列的資料繫結選項按鈕。 當資料列向使用者顯示資料,且不在編輯模式中時, RadioButton 控制項會停用。 當資料列處於編輯模式時,例如當使用者選擇更新控制項中的資料 GridView 列時, RadioButton 控制項會轉譯為啟用,以便按一下它。 此範例使用位 AND 運算子,因為資料列狀態可能是一或多個 DataControlRowState 值的組合。

// This method adds a RadioButton control and any other 
// content to the cell's Controls collection.
protected override void InitializeDataCell
    (DataControlFieldCell cell, DataControlRowState rowState) {

  RadioButton radio = new RadioButton();

  // If the RadioButton is bound to a DataField, add
  // the OnDataBindingField method event handler to the
  // DataBinding event.
  if (DataField.Length != 0) {
    radio.DataBinding += new EventHandler(this.OnDataBindField);
  }

  radio.Text = this.Text;

  // Because the RadioButtonField is a BoundField, it only
  // displays data. Therefore, unless the row is in edit mode,
  // the RadioButton is displayed as disabled.
  radio.Enabled = false;
  // If the row is in edit mode, enable the button.
  if ((rowState & DataControlRowState.Edit) != 0 ||
      (rowState & DataControlRowState.Insert) != 0) {
    radio.Enabled = true;
  }

  cell.Controls.Add(radio);
}
' This method adds a RadioButton control and any other 
' content to the cell's Controls collection.
Protected Overrides Sub InitializeDataCell( _
    ByVal cell As DataControlFieldCell, _
    ByVal rowState As DataControlRowState)

    Dim radio As New RadioButton()

    ' If the RadioButton is bound to a DataField, add
    ' the OnDataBindingField method event handler to the
    ' DataBinding event.
    If DataField.Length <> 0 Then
        AddHandler radio.DataBinding, AddressOf Me.OnDataBindField
    End If

    radio.Text = Me.Text

    ' Because the RadioButtonField is a BoundField, it only 
    ' displays data. Therefore, unless the row is in edit mode, 
    ' the RadioButton is displayed as disabled.
    radio.Enabled = False
    ' If the row is in edit mode, enable the button.
    If (rowState And DataControlRowState.Edit) <> 0 _
        OrElse (rowState And DataControlRowState.Insert) <> 0 Then
        radio.Enabled = True
    End If

    cell.Controls.Add(radio)
End Sub

備註

衍生自 DataControlField 的類型會實作 方法, InitializeCell 將文字和控制項新增至 DataControlFieldCell 屬於使用資料表來顯示使用者介面之資料控制項的物件, (UI) 。 呼叫各自的 CreateChildControls 方法時,這些資料控制項會逐列建立完整的資料表結構資料列。 方法 InitializeCell 是由 InitializeRow 和 等 DetailsView GridView 資料控制項的 方法呼叫。

當您撰寫使用 物件的自訂資料繫結控制項,以使用 DataControlFieldCell 資料或控制項初始化資料表結構的資料格時,請呼叫這個方法。 當您撰寫衍生自 DataControlField 的類別時,請實作這個方法。

適用於

另請參閱