CopyCode The 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, 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;
}
}