Asp.net 自定义RadioButtonList在回发时丢失值

Asp.net 自定义RadioButtonList在回发时丢失值,asp.net,custom-controls,radiobuttonlist,Asp.net,Custom Controls,Radiobuttonlist,我创建了一个自定义RadioButtonList,它根据类似的规则呈现为无序列表 但由于某些原因,在执行回发时未保存所选项目。 我覆盖的唯一方法是Render方法: [ToolboxData( "<{0}:ULRadioButtonList runat=server></{0}:ULRadioButtonList>" )] public class ULRadioButtonList : RadioButtonList { protected override v

我创建了一个自定义RadioButtonList,它根据类似的规则呈现为无序列表

但由于某些原因,在执行回发时未保存所选项目。 我覆盖的唯一方法是Render方法:

[ToolboxData( "<{0}:ULRadioButtonList runat=server></{0}:ULRadioButtonList>" )]
public class ULRadioButtonList : RadioButtonList {
    protected override void Render( HtmlTextWriter writer ) {
        Controls.Clear();

        string input = "<input id={0}{1}{0} name={0}{2}{0} type={0}radio{0} value={0}{3}{0}{4} />";
        string label = "<label for={0}{1}{0}>{2}</label>";
        string list = "<ul>";

        if ( !String.IsNullOrEmpty( CssClass ) ) {
            list = "<ul class=\"" + CssClass + "\">";
        }

        writer.WriteLine( list );

        for ( int index = 0; index < Items.Count; index++ ) {
            writer.Indent++;
            writer.Indent++;

            writer.WriteLine( "<li>" );

            writer.Indent++;

            StringBuilder sbInput = new StringBuilder();
            StringBuilder sbLabel = new StringBuilder();

            sbInput.AppendFormat(
                input,
                "\"",
                base.ClientID + "_" + index.ToString(),
                base.UniqueID,
                base.Items[index].Value,
                (base.Items[index].Selected ? " checked=\"\"" : "")
            );

            sbLabel.AppendFormat(
                label,
                "\"",
                base.ClientID + "_" + index.ToString(),
                base.Items[index].Text
            );

            writer.WriteLine( sbInput.ToString() );
            writer.WriteLine( sbLabel.ToString() );

            writer.Indent = 1;

            writer.WriteLine( "</li>" );

            writer.WriteLine();

            writer.Indent = 1;
            writer.Indent = 1;
        }

        writer.WriteLine( "</ul>" );
    }
}
[ToolboxData(“”)
公共类ULRadioButtonList:RadioButtonList{
受保护的覆盖无效渲染(HtmlTextWriter编写器){
控件。清除();
字符串输入=”;
字符串标签=“{2}”;
字符串列表=“
    ”; 如果(!String.IsNullOrEmpty(CssClass)){ list=“
      ”; } writer.WriteLine(列表); for(int index=0;index); writer.Indent++; StringBuilder sbInput=新建StringBuilder(); StringBuilder sbLabel=新的StringBuilder(); sbInput.AppendFormat( 输入 "\"", base.ClientID+“”+index.ToString(), base.UniqueID, base.Items[index].Value, (base.Items[index]。选中?“checked=\”:“”) ); sbLabel.AppendFormat( 标签 "\"", base.ClientID+“”+index.ToString(), base.Items[index].Text ); WriteLine(sbInput.ToString()); WriteLine(sbLabel.ToString()); writer.Indent=1; writer.WriteLine(“”); writer.WriteLine(); writer.Indent=1; writer.Indent=1; } writer.WriteLine(“
    ”); } }

我忘了什么吗?如果我使用常规ASP.NET RadioButton列表,则在回发后会保存所选项目,因此没有其他内容会覆盖我的值;它与自定义控件有关。

要使这种方法起作用,您必须将生成标记中的
name
(和id)属性与
RadioButtonList
的属性相匹配。代码中明显的问题是
name
属性的值-它应该为每个单选按钮添加索引,例如

 ...
 sbInput.AppendFormat(
            input,
            "\"",
            base.ClientID + "_" + index.ToString(),
            base.UniqueID + index.ToString(),    // <<< note the change here
            base.Items[index].Value,
 ...
。。。
sbInput.AppendFormat(
输入
"\"",
base.ClientID+“”+index.ToString(),

base.UniqueID+index.ToString(),//列表中所有单选按钮的名称属性必须相同,否则我可以选择多个单选按钮,这不是单选按钮的用途。@thomasvb,是的-我建议更改名称属性是错误的-我只是交叉检查,ASP.NET也会生成相同的名称属性。在这种情况下,我认为这个问题可能会发生与ID生成无关。我建议您评论渲染实现,查看生成的ID for单选按钮,并将其与渲染实现生成的ID进行比较。