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 A 包含 . 的文字或控制項DataControlField

cellType
DataControlCellType

這是其中一項 DataControlCellType 價值。

rowState
DataControlRowState

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

rowIndex
Int32

包含 的 DataControlFieldCell 列的索引。

範例

以下程式碼範例示範如何實作 InitializeCell 源自類別 DataControlField 的控制項。 該 RadioButtonField 類別為控制項的每一列 GridView 渲染一個資料綁定的單選按鈕。 當該列顯示資料給使用者且非編輯模式時,該 RadioButton 控制項會被停用。 當列處於編輯模式時,例如使用者選擇更新控制項中的 GridView 列, RadioButton 控制項會被渲染為啟用,方便點擊。 此範例使用位元與運算子,因為列狀態可能是多個 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 ,例如 DetailsViewGridView

當你在撰寫自訂的資料綁定控制項,使用 DataControlFieldCell 物件初始化資料表結構的儲存格時,請呼叫此方法。 當你撰寫由 DataControlField衍生出的類別時,請實作此方法。

適用於

另請參閱