DataControlField.InitializeCell Metod

Definition

Lägger till text eller kontroller i en cells kontrollsamling.

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)

Parametrar

cell
DataControlFieldCell

En DataControlFieldCell som innehåller texten eller kontrollerna i DataControlField.

cellType
DataControlCellType

Ett av DataControlCellType värdena.

rowState
DataControlRowState

Ett av DataControlRowState värdena som anger tillståndet för raden som innehåller DataControlFieldCell.

rowIndex
Int32

Indexet för raden som DataControlFieldCell finns i.

Exempel

Följande kodexempel visar hur du implementerar InitializeCell metoden för en kontroll som härleds från DataControlField klassen. Klassen RadioButtonField renderar en databunden alternativknapp för varje rad i en GridView kontroll. När raden visar data för en användare och inte är i redigeringsläge RadioButton inaktiveras kontrollen. När raden är i redigeringsläge, till exempel när användaren väljer att uppdatera en rad i GridView kontrollen, RadioButton återges kontrollen som aktiverad så att den kan klickas. I det här exemplet används bitvis AND-operatorer eftersom radtillståndet kan vara en kombination av ett eller flera DataControlRowState värden.

// 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

Kommentarer

Typer som härleds från DataControlField implementerar InitializeCell metoden för att lägga till text och kontroller i ett DataControlFieldCell objekt som tillhör en datakontroll som använder tabeller för att visa ett användargränssnitt (UI). Dessa datakontroller skapar den fullständiga tabellstrukturen rad för rad när deras respektive CreateChildControls metoder anropas. Metoden InitializeCell anropas av metoden för InitializeRow datakontroller som DetailsView och GridView.

Anropa den här metoden när du skriver en anpassad databunden kontroll som använder DataControlFieldCell objekt för att initiera cellerna i tabellstrukturen med data eller kontroller. Implementera den här metoden när du skriver en klass som härletts från DataControlField.

Gäller för

Se även