【问题标题】:Custom RadioButtonList loses value on PostBack自定义 RadioButtonList 在 PostBack 上失去价值
【发布时间】:2011-08-19 23:56:06
【问题描述】:

我创建了一个自定义 RadioButtonList,它基于类似的code example for a custom CheckBoxList 呈现为无序列表。

但由于某种原因,在执行回发时我选择的项目没有保存。 我覆盖的唯一方法是渲染方法:

[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>" );
    }
}

我是不是忘记了什么?如果我使用常规的 ASP.NET RadioButtonList,我选择的项目会在回发后保存,所以没有其他东西会覆盖我的值;它与自定义控件有关。

【问题讨论】:

    标签: asp.net custom-controls radiobuttonlist


    【解决方案1】:

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

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

    另外,在 id 生成中不要使用“_”,您应该使用 ClientIdSeparator 属性。

    最后,我必须建议您不要使用这种控件创作方法,因为它本质上很脆弱,即如果底层 ASP.NET 控件生成其标记的方式发生变化,它可能会损坏。例如,在 ASP.NET 中,可以配置客户端 id 生成逻辑,因此您的 id 生成逻辑可能无法正常工作以匹配基本控件生成的原始标记。

    【讨论】:

    • 列表中所有单选按钮的名称属性必须相同,否则我可以选择多个单选按钮,这不是单选按钮的用途。
    • @thomasvb,是的-我建议更改名称属性是错误的-我只是交叉检查了,ASP.NET 也生成了相同的名称属性。在这种情况下,我认为该问题可能与 ID 生成有关。我建议您评论您的渲染实现并查看为单选按钮生成的 ID,并将其与您的渲染实现生成的 ID 进行比较。
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多