DataControlField.InitializeCell Method (DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)


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


Type: System.Web.UI.WebControls.DataControlFieldCell

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

Type: System.Web.UI.WebControls.DataControlCellType

One of the DataControlCellType values.

Type: System.Web.UI.WebControls.DataControlRowState

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

Type: System.Int32

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;


.NET Framework
Available since 2.0
Return to top