1

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="" />
SRock2016
  • 123
  • 1
  • 2
  • 5

1 Answers1

0

Your bug

I don't know what's wrong with your code because I can't debug your code for see what happens on your VB code. If you don't know how you can debug your code, please watch this YouTube video about debugging.


My suggestion

I suggest you to place all your check boxes onto a panel and loop over all the items of that control. I go check if each control is a RadioButton. if yes, cast that onto a RadioButton and check if the RadioButton is checked. If yes, counter can be display in your label.

The counter counts witch RadioButton is checked and is also the output you can show on your label. See also code for more information for what I do.

Don't forget to add one to the counter or it will stay on the same value.

Here is the VB code:

Dim counter As Integer = 0

For Each contr As Control In pnlAnswers.Controls 'loop over each control on the panel

    If TypeOf contr Is RadioButton Then 'Check if contr is a RadioButton 
        
        RadioButton rdb = CType(contr, RadioButton) 'if yes cast it to a RadioButton

        If rdb.Checked Then 'If checked

            lblOutput.Text = counter.ToString() 'Place the tag value on the output label
            Return

        End If

        counter += 1
    End If
Next

Place also your labels outside the panel. Note that it has no impact for the code behind if the labels are inside the panel. The loop will skip them because a label is not a type of a radio button. But it beter that the labels are outside when you add some features to your application.

Here is the ASP code:

<asp:Label runat="server" ID="QuestionID" Text='<%# Eval("QuestionID")%>' />
<asp:Label runat="server" ID="Question" Text='<%# Eval("Question")%>' /><br />

<asp:panel ID="pnlAnswers" runat="server">
    <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" />
</asp:panel>

P.S.: The tags I've added on a previous update aren't necessary now.


Code clean up

I've also removed this line of code:

Dim qID As Label = row.FindControl("QuestionID")

I can't find any line where you use this line so it's overkill to find your label QuestionID.


Notes

It's a long time ago I've used VB.NET and ASP.NET web forms so please comment me if the code is not working.

#SOreadytohelp

Community
  • 1
  • 1
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • Hi, the reason I am trying to assign them values 1-4 is because down the line I will be checking the values against values within my database. I have a CorrectAns fieild which are integer values ranging from 1-4. So you see why I want the radiobuttons having values 1-4 so I can match them against the correct answer data in the database and if the user has selected the correct radiobutton answer I want to output it to a label. – SRock2016 Feb 27 '16 at 17:06
  • @SRock2016: well you can make a counter and every time you loop. See my answer for more information. You can see it in a minute. – H. Pauwelyn Feb 27 '16 at 17:17