Showing/Hiding a Panel in a Repeater

  • Thread starter Kevin Humphreys
  • Start date
K

Kevin Humphreys

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.
 
M

MikeS

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top