Showing/Hiding a Panel in a Repeater

Discussion in 'ASP .Net Web Controls' started by Kevin Humphreys, Nov 9, 2006.

  1. Hi,
    I am trying to show and hide different panels in a row of a repeater pending
    on the record that is returned.
    Inside in the <ItemTemplate> I have a number of Panels and a function called
    ShowPanel.
    If the field "Panel" returns Panel1 then I was to show Panel1 and hide
    Panel2, Panel3 and Panel 4.
    If the field "Panel" returns Panel2 then I was to show Panel2 and hide
    Panel1, Panel3 and Panel 4 etc.

    <asp:repeater id="Repeater1" runat="server">
    <ItemTemplate>
    <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
    <asp:panel id="Panel1" runat="server">Panel1</asp:panel>
    <asp:panel id="Panel2" runat="server">Panel2</asp:panel>
    <asp:panel id="Panel3" runat="server">Panel3</asp:panel>
    <asp:panel id="Panel4" runat="server">Panel4</asp:panel>
    <%# ShowPanel(Container.DataItem("Panel")) %>
    </div>
    </ItemTemplate>
    </asp:repeater>

    The ShowPanel funciton is as follows

    Public Function ShowPanel(ByVal gender As String) As String
    Select Case gender
    Case "Panel1"
    Panel1.Visible = True
    Panel2.Visible = False
    Panel3.Visible = False
    Panel4.Visible = False
    Case "Panel2"
    etc...
    End Select
    Return Nothing
    End Function

    However when I do this I get the following error message
    "System.NullReferenceException: Object reference not set to an instance of
    an object."

    Any ideas?

    You help is much appreciated.

    Regards,
    Kevin Humphreys.
    Kevin Humphreys, Nov 9, 2006
    #1
    1. Advertising

  2. Kevin Humphreys

    MikeS Guest

    I think you will have to use FindControl.

    <%@ Page Language="VB" %>

    <%@ Import Namespace="System.Data" %>

    <script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    If IsPostBack = False Then
    Repeater1.DataSource = GetData()
    Repeater1.DataBind()
    End If
    End Sub

    Private Function GetData() As DataTable
    Dim dt As New DataTable
    dt.Columns.Add("ID")
    dt.Columns.Add("Text")
    For i As Integer = 1 To 10
    dt.Rows.Add(i, "Item " & i)
    Next
    Return dt
    End Function

    Protected Sub dlOption_SelectedIndexChanged(ByVal sender As Object,
    ByVal e As System.EventArgs)
    Select Case dlOption.SelectedItem.Text
    Case "Panel A"
    Toggle("PanelA", "PanelB")
    Case "Panel B"
    Toggle("PanelB", "PanelA")
    End Select
    End Sub

    Private Sub Toggle(ByVal onID As String, ByVal offID As String)
    For Each ri As RepeaterItem In Repeater1.Items
    Dim pOn As Panel = ri.FindControl(onID)
    Dim pOff As Panel = ri.FindControl(offID)
    pOn.Visible = True
    pOff.Visible = False
    Next
    End Sub

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList runat="server" ID="dlOption"
    AutoPostBack="true"
    OnSelectedIndexChanged="dlOption_SelectedIndexChanged">
    <asp:ListItem Text="" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Panel A"></asp:ListItem>
    <asp:ListItem Text="Panel B"></asp:ListItem>
    </asp:DropDownList>
    <hr />
    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# Eval("ID")
    %>'></asp:Label><br />
    <asp:Label runat="server" Text='<%# Eval("Text")
    %>'></asp:Label><br />
    <asp:panel runat="server" ID="PanelA"
    Visible="false">
    This is panel A
    </asp:panel>
    <br />
    <asp:panel runat="server" ID="PanelB"
    Visible="false">
    This is panel B
    </asp:panel>
    <hr />
    </ItemTemplate>
    </asp:Repeater>
    </div>
    </form>
    </body>
    </html>
    MikeS, Nov 10, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Cheng[MSFT]

    Re: Hiding panel controls from Javascript

    Steven Cheng[MSFT], Jan 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    23,745
    James Lankford
    Jan 9, 2004
  2. Replies:
    6
    Views:
    6,291
    Wanda Li
    Mar 18, 2005
  3. dotnetjunkie
    Replies:
    2
    Views:
    1,597
    priyadharsini
    May 6, 2008
  4. Matt
    Replies:
    2
    Views:
    377
  5. Ste
    Replies:
    41
    Views:
    783
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page