【问题标题】:How can i retrieve selected values for a radiobutton list如何检索单选按钮列表的选定值
【发布时间】:2014-07-07 20:09:24
【问题描述】:

我正在为 Sharepoint 开发一个随机测验生成器,但在检查所选答案是否与正确答案匹配时,我遇到了一个小问题。

我有一个声明为全局的单选按钮列表。

public static RadioButtonList[] RadioButtonList = new RadioButtonList[5]; //5 elements are enough, as i'll stick to a 5-question quiz for now.

然后,在填充下拉列表时,我正在创建一个新对象并将项目(答案)添加到列表中。

for (var i=0; ... )
...
RadioButtonList[i] = new RadioButtonList();                 
RadioButtonList[i].Items.Add(SPListItemCollection[index]["Column"].ToString());
...

到目前为止一切正常。以上都发生在生成代码的按钮的点击事件中。

我的问题出现在以下事件中,针对“比较”按钮。 我正在尝试将选定的单选按钮值与存储在标签中的另一个值进行比较,该值代表正确的答案。问题是,以下条件不起作用:

for (var index ... )
....
if (RadioButtonList[index].SelectedValue.Equals(label_Response[index].Text))
...

“RadioButtonList[index].SelectedValue”始终为空,调试时出现 NullReferenceException。

如果有人能帮我解决这个问题,或者知道更好的解决方案,我将非常感激。

提前致谢, 卡琳。

【问题讨论】:

    标签: c# sharepoint radiobuttonlist


    【解决方案1】:

    这只有在所有单选按钮都被选中时才有效。当一次只能选择一个时,您将使用 foreach 单选按钮。 尝试检查不为空。

    for (var index ... )
    ....
    if (RadioButtonList[index].SelectedValue!=null &&  RadioButtonList[index].SelectedValue.Equals(label_Response[index].Text))
    ...
    

    另一件事你怎么能确定label_Response[index] 将匹配RadioButtonList[index]?恕我直言,这是一个巨大的假设。

    【讨论】:

    • 是的,他们会的。它们填充在同一个索引上,并且在调试时,如果选择了正确的答案,则 label_Response[index] 具有 RadioButtonList[index] 应该具有的值。
    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 2023-04-03
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多