1

this is my code to know, which radio-button has been selected, but getting error at line 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

There are 4 radio-button with ID rb_option1,rb_option2,rb_option3 and rb_option4.

Update

<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>

Thank in advance !!

Ravi
  • 30,829
  • 42
  • 119
  • 173

2 Answers2

3

Why don't you use a RadioButton list? Then you can iterate through your radiobutton's in the following manner:

HTML:

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Value="1">Radio 1</asp:ListItem>
    <asp:ListItem Value="2">Radio 2</asp:ListItem>
    <asp:ListItem Value="3">Radio 3</asp:ListItem>
    <asp:ListItem Value="4">Radio 4</asp:ListItem>
</asp:RadioButtonList>

Code:

For i As Integer = 1 To 4
    For Each li As ListItem In RadioButtonList1.Items
        If li.Selected Then
               selected_option = li.Value
        End If
    Next
Next

Apologies if my VB.NET syntax is wrong (used to C#).

SurinderBhomra
  • 2,169
  • 2
  • 24
  • 49
  • i'm using it in repeater, i felt easy to code using radio-button. – Ravi Aug 02 '12 at 13:45
  • In my opinion, using a RadioButtonList would be much easier since you are grouping all your radio buttons together and can iterate through them all easily. I've updated my post to hopefully make it simpler to understand. – SurinderBhomra Aug 02 '12 at 13:49
0

Change this line:

Dim rdbName As String = "rb_option" & i

to this:

Dim rdbName As String = "ctl00_cpExamMaster_Repeater1_ctl01_rb_option" & i
Channs
  • 2,091
  • 1
  • 15
  • 20
  • If you do a View Source of the page, what are the ID's of the radio buttons you see? – Channs Aug 02 '12 at 14:06
  • for 3rd radio-button `ctl00_cpExamMaster_Repeater1_ctl01_rb_option3` – Ravi Aug 02 '12 at 14:08
  • In the code-behind, while debugging, examine `Repeater1.Controls` for radio buttons and check their ID's. – Channs Aug 02 '12 at 14:34
  • didn't get you, please can you tell me, what should i do ? – Ravi Aug 02 '12 at 14:40
  • 1
    Add a breakpoint in the above code. Start debugging. Use the Quick Watch window to check the items in `Repeater1.Controls`. You should come across the 4 radio buttons and their ID's. – Channs Aug 02 '12 at 14:46
  • 1
    @coders Is your repeater actually getting populated with data? – SurinderBhomra Aug 02 '12 at 15:19
  • yes off-course, the error showing, when i'm submitting the data. – Ravi Aug 02 '12 at 15:22
  • i checked the code `Dim rdbName As String = "rb_option" & i.ToString ' MsgBox(rdbName) Dim rdb As New RadioButton() rdb = CType(Repeater1.FindControl(rdbName), RadioButton) MsgBox(rdb)` in the message box, it is showing nothing or null, is this helpful ?? – Ravi Aug 02 '12 at 15:29