Asp.net extends the code for adding a single-choice button column in The gridview.

Source: Internet
Author: User

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;
}
}

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.