I am getting the output of 0
and I don't know what I am doing wrong. I using the findcontrol method to find the IDs within the Gridview and declaring them as radiobuttons, then I am trying to use an if statement to assign the checked radio button a value, then output that value to a label.
vb code
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim numOutput As Integer
For Each row As GridViewRow In GridView1.Rows
Dim qID As Label = row.FindControl("QuestionID")
Dim rd1 As RadioButton = TryCast(row.FindControl("answer1"), RadioButton)
Dim rd2 As RadioButton = TryCast(row.FindControl("answer2"), RadioButton)
Dim rd3 As RadioButton = TryCast(row.FindControl("answer3"), RadioButton)
Dim rd4 As RadioButton = TryCast(row.FindControl("answer4"), RadioButton)
If rd1.Checked = True Then
numOutput = 1
ElseIf rd2.Checked = True Then
numOutput = 2
ElseIf rd3.Checked = True Then
numOutput = 3
ElseIf rd4.Checked = True Then
numOutput = 4
End If
Next
lblOutput.Text = numOutput
End Sub
Source code
<asp:GridView ShowHeader="false" AutoGenerateColumns="false" ID="GridView1" 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="a" Text='<%# Eval("answer1")%>' runat="server" ID="answer1" /><br />
<asp:RadioButton GroupName="a" Text='<%# Eval("answer2")%>' runat="server" ID="answer2" /><br />
<asp:RadioButton GroupName="a" Text='<%# Eval("answer3")%>' runat="server" ID="answer3" /><br />
<asp:RadioButton GroupName="a" Text='<%# Eval("answer4")%>' runat="server" ID="answer4" /><hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:Label ID="lblOutput" runat="server" Text="" />