0

I am trying to find a radiobutton control within a gridview but I keep getting the output of 0 for whatever radiobutton I select. I want each radiobutton to be assigned the value of 1-4 and then whatever radiobutton selected, it will be outputted to a label.

VB code

 Dim SelectNumber As Integer

 For Each row As GridViewRow In MyGridView.Rows

        Dim radbtn1 As RadioButton = TryCast(row.FindControl("a1"), RadioButton)
        Dim radbtn2 As RadioButton = TryCast(row.FindControl("a2"), RadioButton)
        Dim radbtn3 As RadioButton = TryCast(row.FindControl("a3"), RadioButton)
        Dim radbtn4 As RadioButton = TryCast(row.FindControl("a4"), RadioButton)


        If radbtn1.Checked = True Then
            SelectNumber = 1
        ElseIf radbtn2.Checked = True Then
            SelectNumber = 2
        ElseIf radbtn3.Checked = True Then
            SelectNumber = 3
        ElseIf radbtn4.Checked = True Then
            SelectNumber = 4
        End If


    Next

    lblOutput.Text = SelectNumber

Source code

 <asp:GridView ShowHeader="false" AutoGenerateColumns="false" ID="MyGridView" runat="server" GridLines="None">
        <Columns>
            <asp:TemplateField>

                <ItemTemplate>

                    <asp:Label runat="server" ID="QuestionID" Text='<%# Eval("QuestionID")%>' />
                    <asp:Label runat="server" ID="Question" Text='<%# Eval("Question")%>' /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a1")%>' runat="server" ID="ans1" /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a2")%>' runat="server" ID="ans2" /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a3")%>' runat="server" ID="ans3"  /><br />                  
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a4")%>' runat="server" ID="ans4" /><hr />
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>

    </asp:GridView>

<asp:Button ID="btnSubmit" runat="server" Text="Complete" />
   <asp:Label ID="lblOutput" runat="server" Text="" />
SRock2016
  • 123
  • 1
  • 2
  • 5
  • 1
    Where is the code located that you've shown? I'm pretty sure that the reason for this issue is that you databind the grid on every postback instead of only `If Not Page.IsPostback`. Then no one is checked and the `SelectNumber` keeps it's default value which is `0` – Tim Schmelter Mar 01 '16 at 13:39
  • That resolved the issue - Thanks. – SRock2016 Mar 01 '16 at 13:44

1 Answers1

1

I'm pretty sure that the reason for this issue is that you databind the grid on every postback instead of only If Not Page.IsPostback. Then no one is checked and the SelectNumber keeps it's default value which is 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        ' DataBind the GridView here or call a method therefore '
    End If
End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939