简体   繁体   English

如何检索单选按钮列表的选定值

[英]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.

相关问题 如何从已定义的单选按钮组中找到选定的单选按钮 - How can I find selected radiobutton from defined group of radiobutton 如何在ASP上检索单选按钮选择的值:在JavaScript中单击按钮 - How to retrieve radiobutton selected value on asp: button click in javascript 从枚举中加载radiobutton列表时,如何在每个单选按钮旁边显示文本? - How can I display text beside each radiobutton when loading a radiobutton list from an enumeration? 如何在WPF Mvvm中检索动态生成的单选按钮的选定单选按钮 - How to retrieve selected RadioButton of dynamic generated RadioButtons in WPF Mvvm 如何以编程方式向页面添加未知数量的下拉列表,并在提交时使用c#检索所选值 - How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit 我如何在扩展身份模型中返回单选按钮选定的值 - How can i return radiobutton selected value within extended idenity model within 我该如何绑定到某个组中是否选择了任何“ RadioButton”? - How can I bind to whether any/none `RadioButton` of a certain group is selected? 如何在ASP.NET中找到所选的RadioButton值? - How can I find the selected RadioButton's value in ASP.NET? 在MVC中获取选定的单选按钮值 - Get selected radiobutton values in MVC 如何检索列表框中选定项目的选定值? - How to retrieve selected values for selected items in a ListBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM