views:

771

answers:

2

Good morning stackoverflow!

I have a little problem I'm trying to work out thats bugging the life out of me!

On my .aspx page i want to be able to show and hide certain panels depending on user selections (radiobuttonlists).

For example in my aspx page i have;

<form id="form1" runat="server">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:RadioButtonList>

    <asp:Panel ID="Panel1" runat="server" Width="50%">
        Visible or not visible depending on radio choice<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </asp:Panel>
    </form>

Then in my aspx.vb i have;

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If RadioButtonList1.SelectedItem.Equals(Nothing) Then
            Panel1.Visible = False
        Else
            RadioButtonList1.SelectedItem.Equals(3)
            Panel1.Visible = True
        End If

    End Sub

I've also tried a few different variants of this code, along with trying a select statement. If anyone could offer any advise on how to work this one out it greatly appreciate it

Thanks a lot, Phil

EDIT:

After further attempts and some reading on msdn I now have;

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



' Show or Hide the Panel contents.
    If RadioButtonList1.SelectedItem.Equals(3) Then
        Panel1.Visible = True
    Else
        Panel1.Visible = False
    End If

End Sub

But when I try to run the code I get;

"Object reference not set to an instance of an object" on this line If RadioButtonList1.SelectedItem.Equals(3) Then

A: 
panel.enabled = false

might do the trick, otherwise you can always try to use javascript or jquery or something like that to either set

display = none

or call (with jquery)

$('#Panel1').hide();
SilverSkin
A: 

You've got a few reasons that's happening. First, there's no selected item, so when you're trying to do "RadioButtonList1.SelectedItem.Equals(3)", SelectedItem is Nothing, so there's no object to perform the Equals comparison.

Next, you're trying to see if the SelectedItem is equal to 3. The SelectedItem will be a ListItem object. You want to compare the Value property of that object: RadioButtonList1.SelectedItem.Value

Last, since RadioButtonList1.SelectedItem.Value returns a string, that .Equals will never be true because you're asking if the number 3 is the same as the string "3".

To fix it, throw in a check to see if there is a selected value and then compare the RadioButtonList1.SelectedItem.Value to the string "3":

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

    ' Set the panel to hidden by default
    Panel1.Visible = False

    ' Check to see if there's a selected value
    If Not RadioButtonList1.SelectedItem Is Nothing Then
        ' there is.. check to see if the value is correct
        If RadioButtonList1.SelectedItem.Value = "3" Then
            ' it is.. show the panel!
            Panel1.Visible = True
        End If
    End If

End Sub
Wayne