我正在尝试将单选按钮与 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);
}
}
}
}
谁能帮忙?