Export (0) Print
Expand All

DataControlField.InitializeCell Method

Adds text or controls to a cell's controls collection.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual void InitializeCell (
	DataControlFieldCell cell,
	DataControlCellType cellType,
	DataControlRowState rowState,
	int rowIndex
)
public void InitializeCell (
	DataControlFieldCell cell, 
	DataControlCellType cellType, 
	DataControlRowState rowState, 
	int rowIndex
)
public function InitializeCell (
	cell : DataControlFieldCell, 
	cellType : DataControlCellType, 
	rowState : DataControlRowState, 
	rowIndex : int
)
Not applicable.

Parameters

cell

A DataControlFieldCell that contains the text or controls of the DataControlField.

cellType

One of the DataControlCellType values.

rowState

One of the DataControlRowState values, specifying the state of the row that contains the DataControlFieldCell.

rowIndex

The index of the row that the DataControlFieldCell is contained in.

Types derived from DataControlField implement the InitializeCell method to add text and controls to a DataControlFieldCell object that belongs to a data control that uses tables to display a user interface (UI). These data controls create the complete table structure row by row when their respective CreateChildControls methods are called. The InitializeCell method is called by the InitializeRow method of data controls such as DetailsView and GridView.

Call this method when you are writing a custom data-bound control that uses DataControlFieldCell objects to initialize the cells of the table structure with data or controls. Implement this method when you are writing a class derived from DataControlField.

The following code example demonstrates how to implement the InitializeCell method for a control that derives from the DataControlField class. The RadioButtonField class renders a data-bound radio button for every row in a GridView control. When the row is displaying data to a user and is not in edit mode, the RadioButton control is disabled. When the row is in edit mode, for example when the user chooses to update a row in the GridView control, the RadioButton control is rendered as enabled so that it can be clicked. This example uses bitwise AND operators, because the row state might be a combination of one or more DataControlRowState values.

// 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 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 (get_DataField().get_Length() != 0) {
        radio.add_DataBinding(new EventHandler(this.OnDataBindField));
    }
    radio.set_Text(this.get_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.set_Enabled(false);
    // If the row is in edit mode, enable the button.
    if (((int)(rowState & DataControlRowState.Edit) != 0) || ((int)(
        rowState & DataControlRowState.Insert) != 0)) {
        radio.set_Enabled(true);
    }
    cell.get_Controls().Add(radio);
} //InitializeDataCell

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft