asp.net extension GridView Add code for radio button Columns _ Practical Tips

Source: Internet
Author: User
Copy Code code as follows:

<summary>
radio button Columns
</summary>
<remarks>
If GroupName is not set, the ID of the GridView is used as the GroupName
If DataField is not set, use rowindex as value
Allow dataformatstring format data to be set
</remarks>
[AspNetHostingPermission (SecurityAction.Demand,
Level = Aspnethostingpermissionlevel.minimal)]
public class Radiobuttonselectfield:datacontrolfield
{
<summary>
radio button group name, default to ClientID of GridView
</summary>
[Description ("radio button group name, default to ClientID of GridView")]
public string Radiobuttongroupname
{
Get
{
return this. viewstate["Radiobuttongroupname"] as String;
}
Set
{
This. viewstate["Radiobuttongroupname"] = value;
}
}

<summary>
The style of the radio button
</summary>
[Description (Style of radio buttons)]
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 bind)]
public string DataField
{
Get
{
return this. viewstate["DataField"] as String;
}
Set
{
This. viewstate["DataField"] = value;
}
}

<summary>
The format of the data expression to bind
</summary>
[Description (format of data expressions to bind)]
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>
Adding 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.Parent.Parent.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;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.