Asp.net 如何从动态创建的单选按钮列表中查找选定的单选按钮?
嘿,我正在制作在线考试表格,其中我创建了一份带有多项选择题的试卷,所以对于这个创建的动态标签和radiobuttonlist,现在我尝试检查用户选择的答案是否正确,所以为此我创建了一个列表,其中包含所有radiobuttonlist,以及一个列表,列出了所有正确答案,以及当用户按“提交”按钮->Asp.net 如何从动态创建的单选按钮列表中查找选定的单选按钮?,asp.net,vb.net,nullreferenceexception,Asp.net,Vb.net,Nullreferenceexception,嘿,我正在制作在线考试表格,其中我创建了一份带有多项选择题的试卷,所以对于这个创建的动态标签和radiobuttonlist,现在我尝试检查用户选择的答案是否正确,所以为此我创建了一个列表,其中包含所有radiobuttonlist,以及一个列表,列出了所有正确答案,以及当用户按“提交”按钮-> dim answerList as List(Of String) dim radionButtonList as List(Of RadioButtonList) Protected Sub But
dim answerList as List(Of String)
dim radionButtonList as List(Of RadioButtonList)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For i As Integer = 0 To radioButtonList.Count - 1
if String.Compare(radioButtonList(i).SelectedItem.ToString(), answerList(i)) <> 0 than
MsgBox("Wrong Answer")
End If
Next
End Sub
dim应答器列表作为列表(字符串)
dim Radio按钮列表作为列表(Radio按钮列表)
受保护的子按钮1\u单击(ByVal sender作为对象,ByVal e作为事件参数)处理按钮1。单击
对于i作为整数=0的radioButtonList.Count-1
如果String.Compare(radioButtonList(i).SelectedItem.ToString(),answerList(i))比0
MsgBox(“错误答案”)
如果结束
下一个
端接头
但当我单击按钮时,会出现一个错误“System.NullReferenceException:对象引用未设置为对象的实例。”
我想当我按下按钮时,页面会刷新,所以你知道我该如何解决这个问题吗?好吧,我自己弄明白了,只有使用javascript才有可能。因此,我在asp.net中编写js代码,并在运行时使用文档模型的两个函数
document.getElementById()
和document.getElementsByTagName()
scriptText&=“函数checkRadio(){”+_
“var sel=新数组();”
(变量i=1;i)的脚本文本&=”
scriptText &= "function checkRadio(){" + _
"var sel = new Array();"
scriptText &= "for(var i = 1; i <= 3; i++) {" + _
"var ele = document.getElementById('MainContent_RadioButtonList' + i);" + _
"for(var j = 0; j < 4; j++) {" + _
" if(ele.getElementsByTagName('input')[j].checked) { " + _
" sel[i] = ele.getElementsByTagName('input')[j].value; console.log(sel[i]);}}} "
scriptText &= ""
scriptText &= "}"
ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"CounterScript", scriptText, True)