复制代码 代码如下:
/// <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;
}
}
标签:
asp.net,扩展GridView,单选按钮

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。