Beginner question regarding radiobuttonlist and binding to a hashtable

sgt_b2002

New member
Joined
Feb 22, 2011
Messages
2
Programming Experience
Beginner
Hopefully this is something simple, but I'm failry new to VB and ASP.NET and I'm trying to figure something out. Lets say I have two radiobuttonlists. One is populated in in asp.net while the other is populated via a hashtable in the vb.net code behind.

I then have a button that displays the selected items from the two radiolists when its clicked. Seems simple, but for the life of me I cannot get the radiobuttonlist that is populated from the hashtable to work properly. When I click the button I get the "Object reference not set to an instance of an object" error like the object isn't even there.

Anyway, here's the code. I'm very new to VB so I'm sure this is something simple.

ASP.NET
VB.NET:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
   <asp:ListItem Value="0001">test1</asp:ListItem>
   <asp:ListItem Value="0002">test2</asp:ListItem>
</asp:RadioButtonList>

<asp:RadioButtonList ID="RadioButtonList2" runat="server">
</asp:RadioButtonList>

<asp:Button ID="Button1" runat="server" Text="Button" />

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
VB.NET
VB.NET:
Public Class Test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Label1.Text = "Radio Table Test"

            Dim ht1 As Hashtable = New Hashtable

            ht1.Add("Radio Item 1", "001")
            ht1.Add("Radio Item 2", "002")
            ht1.Add("Radio Item 3", "003")
            ht1.Add("Radio Item 4", "004")

            RadioButtonList2.DataSource = ht1
            RadioButtonList2.DataTextField = "key"
            RadioButtonList2.DataValueField = "value"
            RadioButtonList2.DataBind()

            Button1.Text = "Submit"
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Label2.Text = "Radio Button Value = " & RadioButtonList1.SelectedItem.Value
        Label3.Text = "Radio Button List 2 Value = " & RadioButtonList2.SelectedItem.Value

    End Sub
End Class

If I comment out the Label3.Text line at the end everything with the first radiobuttonlist works just fine and the selected value is returned. Its only when I uncomment the Label3.Text line at the end and reference RadioButtonList2.SelectedItem.Value that I get problem.

Thanks for any help/tips. :)
 
When you bind the datasource items on postback the SelectedItem viewstate is lost.
VB.NET:
If Not IsPostBack Then ' bind items source
 
Thank you so much JohnH. I figured I was just missing some fundamental knowledge on the subject. This certainly solved the problem I was seeing. Thanks for pointing me in the right direction! :)
 
Back
Top