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 的文本或控件。

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 方法时,这些数据控件按行创建完整的表结构行。 该方法 InitializeCellInitializeRow 数据控件的方法(如 DetailsViewGridView)调用。

编写自定义数据绑定控件时调用此方法,该控件使用 DataControlFieldCell 对象通过数据或控件初始化表结构的单元格。 编写派生自 DataControlField的类时实现此方法。

适用于

另请参阅