简体   繁体   中英

How to findout selected radiobutton from radiobutton list which is created dynamically?

Hey i working on online examination form in which i created a paper with multiple choice question so for this created dynamic label and radiobuttonlist now i try to check the answer selected by user whether it right or wrong so for this i created a list which contain all the radiobuttonlist in it and a list all the right answer and when user press "submit" button ->

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

but when i click the button is gives an error " System.NullReferenceException: Object reference not set to an instance of an object. " i guess page is refreshed when i press the button so any idea how i solve this?

well i figured it out by myself it is only possible by using javascript. So i write js code in the asp.net and register it at the runtime and using document model's two function document.getElementById() and document.getElementsByTagName()

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)

"MainContent_" is my content holder so if u don't have content holder u can simply put the "RadioButtonList" or you can simply use getElementsByTagName() but it will give you the object with input tag like button, textfield, radio etc so yo have to first check the type and then work on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM