【问题标题】:Radiobuttonlist selecteditem is always nullRadiobuttonlist selecteditem 始终为空
【发布时间】:2014-01-04 02:24:38
【问题描述】:

测验项目,一个问题有一个单选按钮列表,其中包含 2 个项目(T/F)或 4 个项目(a、b、c、d) 问题的数量不同。面板用于一次仅显示一个问题(显示/隐藏)。 回答完所有问题后,用户单击提交按钮,所有答案都应保存在数据库中。在 selecteditem 后面的代码中始终为 null 且 value 始终为空字符串。

<asp:DataList ID="dtQuestion" runat="server" RepeatDirection="Vertical" OnItemDataBound="FormatDataListRow" >
        <ItemTemplate>
            <asp:Panel id="panel" runat="server" BorderColor="#536895" BorderStyle="Solid" BorderWidth="1" style="display: none;" EnableViewState="true">
                <asp:Label id="lblQuestionDesc" runat="server" Text="" ></asp:Label>
                <asp:RadioButtonList id="rbl" runat="server" EnableViewState="true" >    </asp:RadioButtonList>
                </asp:Panel>
            </ItemTemplate>                
</asp:DataList>

提交点击。我调用了一个在页面上搜索 RBL 的函数,我能够看到它们的正确 ID 和列表项,但没有选择任何内容。

 string id;
       if     (c.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButtonList"))
               {
                   if (c.ID != "rbl")
                   {
                    id = c.ID;
                    al.Add(id + "," + ((RadioButtonList)c).SelectedItem.Value); //SelectedValue);  //
                }
            }

【问题讨论】:

    标签: selecteditem radiobuttonlist


    【解决方案1】:

    您似乎在页面加载时填充 RadioButtonList - 如果是这样 - 请确保您将 RadioButtonList 的人口包围在一个 如果/那么/回发块: 如果不是 Page.IsPostBack 则 ' 填充你的 RBL 结束如果

    例如:

            if (!IsPostBack)
            {
                loadradiobuttonlist();
            }
    

    【讨论】:

      【解决方案2】:

      尝试像这样访问提交的值:

      this.Request.Form[((RadioButtonList)c).UniqueID]
      

      在调试期间,您始终可以检查您在 this.Request.Form 集合中获得的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        • 1970-01-01
        • 2016-07-15
        • 1970-01-01
        • 2012-01-19
        • 2017-07-31
        • 2019-07-18
        相关资源
        最近更新 更多