欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 扩展GridView 增加单选按钮列的代码

程序员文章站 2024-03-08 10:28:46
复制代码 代码如下:/// /// 单选按钮列 /// /// /// 如...
复制代码 代码如下:

/// <summary>
/// 单选按钮列
/// </summary>
/// <remarks>
/// 如果没有设置groupname,则使用gridview的id作为groupname
/// 如果没有设置datafield,则使用rowindex作为value
/// 允许设置dataformatstring格式化数据
/// </remarks>
[aspnethostingpermission(securityaction.demand,
level = aspnethostingpermissionlevel.minimal)]
public class radiobuttonselectfield : datacontrolfield
{
/// <summary>
/// 单选按钮组名,默认去gridview的clientid
/// </summary>
[description("单选按钮组名,默认去gridview的clientid")]
public string radiobuttongroupname
{
get
{
return this.viewstate["radiobuttongroupname"] as string;
}
set
{
this.viewstate["radiobuttongroupname"] = value;
}
}

/// <summary>
/// 单选按钮的样式
/// </summary>
[description("单选按钮的样式")]
public string radiobuttoncssclass
{
get
{
return this.viewstate["radiobuttoncssclass"] as string;
}
set
{
this.viewstate["radiobuttoncssclass"] = value;
}
}

/// <summary>
/// 要绑定的数据表达式
/// </summary>
[description("要绑定的数据表达式")]
public string datafield
{
get
{
return this.viewstate["datafield"] as string;
}
set
{
this.viewstate["datafield"] = value;
}
}

/// <summary>
/// 要绑定的数据表达式格式
/// </summary>
[description("要绑定的数据表达式格式")]
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>
/// 添加控件
/// </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;
}
}