我有一个单选按钮控件适配器,它尝试将带有 CSS 类的单选按钮控件呈现为输入标记的一部分,而不是作为周围的跨度。
public class RadioButtonAdapter : WebControlAdapter
{
protected override void Render(HtmlTextWriter writer)
{
RadioButton targetControl = this.Control as RadioButton;
if (targetControl == null)
{
base.Render(writer);
return;
}
writer.AddAttribute(HtmlTextWriterAttribute.Id, targetControl.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
writer.AddAttribute(HtmlTextWriterAttribute.Name, targetControl.GroupName); //BUG - should be UniqueGroupName
writer.AddAttribute(HtmlTextWriterAttribute.Value, targetControl.ID);
if (targetControl.CssClass.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, targetControl.CssClass);
}
if (targetControl.Page != null)
{
targetControl.Page.ClientScript.RegisterForEventValidation(targetControl.GroupName, targetControl.ID);
}
if (targetControl.Checked)
{
writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
}
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
}
目前,这非常接近我想要的,唯一的区别是组名属性(标准单选按钮使用内部值 UniqueGroupName,而我只使用 GroupName。我似乎找不到获取 UniqueGroupName 的方法,无论如何,下面的行都应该解决这个问题:
targetControl.Page.ClientScript.RegisterForEventValidation(targetControl.GroupName, targetControl.ID);
带有标准单选按钮的旧 HTML -
<span class="radio">
<input id="ctl00_ctl00_mainContent_RadioButton1" type="radio" value="RadioButton1" name="ctl00$ctl00$mainContent$mygroup"/>
</span>
新渲染——
<input id="ctl00_ctl00_mainContent_RadioButton1" class="radio" type="radio" value="RadioButton1" name="mygroup"/>
问题是回发不起作用 - RadioButton1.Checked 值始终为假。关于如何在回发中获取单选按钮的值的任何想法?