Copy codeThe Code is as follows:
/// <Summary>
/// Single-choice button Column
/// </Summary>
/// <Remarks>
/// If GroupName is not set, use the ID of the GridView as the GroupName
/// If DataField is not set, RowIndex is used as the Value.
/// Allow DataFormatString formatting
/// </Remarks>
[AspNetHostingPermission (SecurityAction. Demand,
Level = AspNetHostingPermissionLevel. Minimal)]
Public class RadioButtonSelectField: DataControlField
{
/// <Summary>
/// The name of the single-choice button group. The ClientID of the GridView is used by default.
/// </Summary>
[Description ("Single-choice button group name, go to the ClientID of the GridView by default")]
Public string RadioButtonGroupName
{
Get
{
Return this. ViewState ["RadioButtonGroupName"] as string;
}
Set
{
This. ViewState ["RadioButtonGroupName"] = value;
}
}
/// <Summary>
/// Single-choice button style
/// </Summary>
[Description ("Single-choice button style")]
Public string RadioButtonCssClass
{
Get
{
Return this. ViewState ["RadioButtonCssClass"] as string;
}
Set
{
This. ViewState ["RadioButtonCssClass"] = value;
}
}
/// <Summary>
/// The data expression to bind
/// </Summary>
[Description ("data expression to be bound")]
Public string DataField
{
Get
{
Return this. ViewState ["DataField"] as string;
}
Set
{
This. ViewState ["DataField"] = value;
}
}
/// <Summary>
/// Format of the data expression to bind
/// </Summary>
[Description ("format of the data expression to be bound")]
Public string DataFormatString
{
Get
{
Return this. ViewState ["DataFormatString"] as string;
}
Set
{
This. ViewState ["DataFormatString"] = value;
}
}
/// <Summary>
/// Return self;
/// </Summary>
/// <Returns> </returns>
Protected override DataControlField CreateField ()
{
Return this;
}
/// <Summary>
/// Add controls
/// </Summary>
/// <Param name = "cell"> </param>
/// <Param name = "cellType"> </param>
/// <Param name = "rowState"> </param>
/// <Param name = "rowIndex"> </param>
Public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
Base. InitializeCell (cell, cellType, rowState, rowIndex );
If (cellType = DataControlCellType. DataCell)
{
Var literal = new Literal ();
If (string. IsNullOrEmpty (this. DataField ))
{
SetLiteralHtml (literal, rowIndex. ToString ());
}
Else
{
Literal. DataBinding + = new EventHandler (literal_DataBinding );
}
Cell. Controls. Add (literal );
}
}
Void literal_DataBinding (object sender, EventArgs e)
{
If (string. IsNullOrEmpty (this. DataField ))
{
Return;
}
Var literal = sender as Literal;
If (literal = null)
{
Return;
}
Var cell = literal. Parent as TableCell;
If (cell = null)
{
Return;
}
Var container = literal. NamingContainer;
If (container = null)
{
Return;
}
Bool foundDataItem;
Var dataItem = DataBinder. GetDataItem (container, out foundDataItem );
If (! FoundDataItem)
{
Return;
}
Var dataValue = null as string;
If (this. DataField. Contains ('.'))
{
DataValue = DataBinder. Eval (dataItem, this. DataField, this. DataFormatString );
}
Else
{
DataValue = DataBinder. GetPropertyValue (dataItem, this. DataField, this. DataFormatString );
}
SetLiteralHtml (literal, dataValue );
}
Private void SetLiteralHtml (Literal literal, string dataValue)
{
Var groupName = this. RadioButtonGroupName;
If (string. IsNullOrEmpty (groupName ))
{
GroupName = literal. Parent. ID;
}
Var cssClass = this. RadioButtonCssClass;
If (! String. IsNullOrEmpty (cssClass ))
{
CssClass = string. Format ("class = \" {0} \ "", this. RadioButtonCssClass );
}
Var selected = false;
Var selectedValue = literal. Page. Request [groupName];
If (string. IsNullOrEmpty (selectedValue) = false)
{
If (string. Compare (selectedValue, dataValue, true) = 0)
{
Selected = true;
}
}
Var rbHtml = string. format ("<input type = \" radio \ "name = \" {0} \ "value = \" {1} \ "{2} {3}/> ",
GroupName,
DataValue,
CssClass,
Selected? "Checked": string. Empty );
Literal. Text = rbHtml;
}
}