[英]How can i retrieve selected values for a radiobutton list
I'm working on a random quiz generator for Sharepoint, and i'm stuck at a small issue, when checking if the selected answer matches the correct answer. 我正在为Sharepoint开发随机测验生成器,并且在检查所选答案是否与正确答案匹配时遇到一个小问题。
i have a radiobutton list declared global. 我有一个单选按钮列表声明为全局。
public static RadioButtonList[] RadioButtonList = new RadioButtonList[5]; //5 elements are enough, as i'll stick to a 5-question quiz for now.
Then, when populating the dropdown list, i'm creating a new object and adding the items (answers) to the list. 然后,在填充下拉列表时,我将创建一个新对象并将项目(答案)添加到列表中。
for (var i=0; ... )
...
RadioButtonList[i] = new RadioButtonList();
RadioButtonList[i].Items.Add(SPListItemCollection[index]["Column"].ToString());
...
Everything works fine till now. 到目前为止一切正常。 All the above happens in the click event for the button that generates the code.
以上所有情况均发生在生成代码的按钮的click事件中。
My problem comes in the following event, made for the Compare button. 我的问题来自以下事件,该事件是“比较”按钮造成的。 I am trying to compare the selected radiobutton value with another value stored in a label, that represents the correct answer.
我正在尝试将选定的单选按钮值与标签中存储的另一个值进行比较,该值代表正确的答案。 The thing is, the following condition does not work:
问题是,以下条件不起作用:
for (var index ... )
....
if (RadioButtonList[index].SelectedValue.Equals(label_Response[index].Text))
...
The "RadioButtonList[index].SelectedValue" is always empty, and i get a NullReferenceException when debugging. “ RadioButtonList [index] .SelectedValue”始终为空,并且在调试时得到NullReferenceException。
If anyone can help me solve this issue, or knows a better solution, i will be very grateful. 如果有人可以帮助我解决这个问题,或者知道更好的解决方案,我将非常感激。
Thanks in advance, Calin. 预先感谢,卡林。
This would work only if ALL the radiobuttons would be selected. 仅当选择所有单选按钮时,此方法才有效。 You are going foreach radiobutton when only one can be selected at one time.
当一次只能选择一个时,您将进入foreach单选按钮。 Try checking for not null.
尝试检查是否为非null。
for (var index ... )
....
if (RadioButtonList[index].SelectedValue!=null && RadioButtonList[index].SelectedValue.Equals(label_Response[index].Text))
...
Another thing how can you be certain that label_Response[index]
will match RadioButtonList[index]
? 另一件事,您如何确定
label_Response[index]
将匹配RadioButtonList[index]
? This is massive assumption IMHO. 这是恕我直言的大规模假设。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.