【发布时间】:2012-05-19 16:37:13
【问题描述】:
我正在尝试将单选按钮与 groupname 属性组合在一起。 由于在渲染时 asp.net 中的命名约定,它不起作用..
所以我尝试继承单选按钮控件并覆盖单选按钮并创建自己的控件..
它解决了分组问题,但由于某种原因它没有处理视图状态...... 所以我所有的单选按钮都是无视图的..
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace CustomControl
{
public class GroupRadioButton : RadioButton
{
public GroupRadioButton()
: base()
{ }
protected override void Render(HtmlTextWriter writer)
{
base.Render(new GroupedRadioButtonTextWriter(writer,this.GroupName));
}
}
public class GroupedRadioButtonTextWriter : HtmlTextWriter
{
private string groupName;
public GroupedRadioButtonTextWriter(HtmlTextWriter baseWriter, string groupName) : base(baseWriter)
{
this.groupName = groupName;
}
public override void AddAttribute(HtmlTextWriterAttribute key, string value)
{
if (key == HtmlTextWriterAttribute.Name)
{
base.AddAttribute(key, this.groupName);
}
else
{
base.AddAttribute(key, value);
}
}
}
}
谁能帮忙?
【问题讨论】:
-
您能解释一下 RadioButton 上的 GroupName 属性对您不起作用的原因吗?对我来说就像一个魅力(至少在我过去使用 WebForms 时)。你用的是什么版本的 ASP.NET WebForms(我刚用 4.0 重新测试过)
-
你试过 RadioButtonList 吗?这将创建一组单选按钮。您需要做的就是将数据源绑定到它。 msdn.microsoft.com/en-us/library/…
标签: c# asp.net radio-button overriding viewstate