这是我要知道的代码,选择了哪个单选按钮,但在第 6 行出现错误。Object reference not set to an instance of an object
Dim selected_option As String = "0"
For i = 1 To 4
Dim rdbName As String = "rb_option" & i
Dim rdb As New RadioButton()
rdb = CType(Me.Page.FindControl(rdbName), RadioButton)
If rdb.Checked = True Then
selected_option = i.ToString
Exit For
End If
Next
有 4 个带有 ID rb_option1
、rb_option2
和rb_option3
的单选按钮rb_option4
。
更新
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="width: 100%; height: 100%; margin-bottom: 50px">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 95%">
<asp:Label ID="lbl_question" CssClass="frm_label" Text='<%# Eval("QUESTION")%>'runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="rb_option1" Checked='<%# IF(Eval("ANSWER")="1",true,false) %>' GroupName="answer" Text='<%# Eval("OPT1") %>' CssClass="frm_label" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="rb_option2" Checked='<%# IF(Eval("ANSWER")="2",true,false) %>' GroupName="answer" Text='<%# Eval("OPT2") %>' CssClass="frm_label" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="rb_option3" Checked='<%# IF(Eval("ANSWER")="3",true,false) %>' GroupName="answer" Text='<%# Eval("OPT3") %>' CssClass="frm_label" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="rb_option4" Checked='<%# IF(Eval("ANSWER")="4",true,false) %>' GroupName="answer" Text='<%# Eval("OPT4") %>' CssClass="frm_label" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
预先感谢 !!