asp.netvb.netweb-applications

Radio Button not being checked vb.net


I'm trying to check/select the radio button on code behind (vb.net) but it's not working. It's not putting a dot on the selected radio button. I don't know why. I know it's getting the right row/selection. But there's no dot the selected radio button.

   Private Function GetRadioButton() As Object
    Dim rObject As Object = Nothing
    Dim rbtnPTypes As RadioButton
    Try
        For Each i As ListViewDataItem In lsvAvailable.Items
            If SessionVars.TYPE_ID = lsvAvailable.DataKeys(i.DataItemIndex).Value.ToString() Then
                rbtnTypes = CType(i.FindControl("rbtnType"), RadioButton)
               rObject = rbtnTypes
            End If
        Next
    Catch ex As Exception
    End Try

    Return rObject
  End Function

This is how I call the function on radio button checked changed():

    If Not IsPostBack Then
            If Not SessionVars.CARD_NUM Is Nothing Then
                txtCardNum.Text = SessionVars.CARD_NUM
                txtBirthDate.Text = SessionVars.DOB                    
                rObject = GetRadioButton()
                rbtn_CheckedChanged(rObject, e)                    
            End If
        End If

This is the function of radio button when check changed:

 Protected Sub rbtn_CheckedChanged(sender As Object, e As EventArgs)

      For Each i As ListViewDataItem In lsvAvailable.Items
            rbtnTypes = CType(i.FindControl("rbtn"), RadioButton)
            rbtnTypes.Checked = False
      Next
        rbtnTypes = CType(sender, RadioButton)
        rbtnTypes.Checked = True
 End Sub

But no MARK of DOT is on the screen, would anyone enlighten me about this? Is it because it's not triggered physically by the user?

Thank you!


Solution

  • Feedback

    Well, it's not clear where SessionVars is being loaded, setup before your posted code uses that made up value/var.

    Also, it is not clear why you using find control here. It is possible that you're using some type of repeating data control (repeater, listview, gridview etc.).

    And you left out the markup for the RadioButton list in question? This makes it VERY difficult to guess and know what you are trying to do here.

    Answer

    However, let's drop in a RadioButton list, and then use codebehind to set that RB list.

    So, a RadioButton list (like a listbox, or even combo box (dropdownlist) can have 2 values.

    The "display" value - the value the user sees.

    The "hidden" value - the value the control returns based on user choice.

    Often, the display value (text), and the hidden value are the same, but often they will not be.

    We might have a drop down list, displays some text but the value might be say a database PK row id.

    And there is a third option - the index chosen! So, if you have 10 values, you can get the "index" 0-9 from that control.

    Let's drop in 3 text boxes, 3 buttons. Each choice will set/change the Radio button list based on the value you enter.

    We have this:

        <div style="float:left">
            <h4>Set RB by Value</h4>
            Enter Value: 2,4,6,8,10
            <br />
            <asp:TextBox ID="txtByValue" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="cmdValueSet" runat="server" Text="Set by Value" 
                OnClick="cmdValueSet_Click"/>
        </div>
    
        <div style="float:left;margin-left:30px">
            <h4>Set RB by Text</h4>
            Enter Text: Poor, Fair, Good, Excellent, 5 stars
            <br />
            <asp:TextBox ID="txtByText" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="cmdTextSet" runat="server" Text="Set by Text" 
                OnClick="cmdTextSet_Click" />
        </div>
    
        <div style="float:left;margin-left:30px">
            <h4>Set RB by index</h4>
            Enter Index:  0,1,2,3,4
            <br />
            <asp:TextBox ID="txtByIndex" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="cmdIndexSet" runat="server" Text="Set by Index"
                OnClick="cmdIndexSet_Click" />
        </div>
    
        <div style="clear:both"></div>
        <br />
    
        <asp:RadioButtonList ID="RadioButtonList1" 
            runat="server">
            <asp:ListItem Value="2">Poor</asp:ListItem>
            <asp:ListItem Value="4">Fair</asp:ListItem>
            <asp:ListItem Value="6">Good</asp:ListItem>
            <asp:ListItem Value="8">Excellent</asp:ListItem>
            <asp:ListItem Value="10">5 Stars</asp:ListItem>
    
        </asp:RadioButtonList>
    

    And thus we get/see this:

    enter image description here

    As noted, there is no need to do a find control.

    However, are you trying to set the radio button list by Value? by text? or by index?

    You have to decide how you want this to work.

    Since you did not share your markup, then we all have to guess here, and post a much more complex solution that is "general" without knowing which road you want to take here.

    Note that some RadioButton list use the text tag, and thus this:

    <asp:RadioButtonList ID="RadioButtonList1" 
        runat="server">
        <asp:ListItem Value="2" Text="Poor"></asp:ListItem>
        <asp:ListItem Value="4" Text="Fair"></asp:ListItem>
        <asp:ListItem Value="6" Text="Good"></asp:ListItem>
        <asp:ListItem Value="8" Text="Excellent"></asp:ListItem>
        <asp:ListItem Value="10" Text="5 Starts"></asp:ListItem>
    </asp:RadioButtonList>
    

    However, the above posted code will work thus the same.

    And as noted, often the RB list will not have BOTH a value and text value (again, just like a listbox, or dropdown (combo) which also has this ability to have 2 values, but often we only use 1 value for such controls, and a RB list works the same).