Repeater in Panel

M

Mick Walker

Hi Folks,

I have a situation where I have a repeater within and ASP Panel on my page.
If the repeater contains no items, then I don't need to show the panel,
however if it does then I do.

I tried the following in the page load event:

// Check to see if we have any new messages
if(repMessages.Items.Count == 0)
{
pnlMessages.Visible = false;
}
if(repMessages.Items.Count > 0 )
{
pnlMessages.Visible = true;
}
However that didn't work, so I figured that the data must be bound after
the page load event fires.So I then tried:

protected void repMessages_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
pnlMessages.Visible = true;
}

Thinking that if an item is being databound, then the Items.Count
property of the repeater must be > 0. However that didn't work either.

So my question is, How can I show and hide a panel which contains a
repeater based on the content (or lack of) of the repeater itself?

Kind Regards
Mick
 
G

Guest

This is the C# language newsgroup. You want to post this in the asp.net
group, as it is really not so much a language question as it is an ASP.NET
framework one.
 
M

Muhammad Naveed Yaseen

1) In code-front make panels Visible property to default invisible (in
cases when data is not bound)

2) In code-front place a header template if it doesn't already (even
an empty one like <HeaderTemplate></HeaderTemplate> would work)

3) In code-behind do like following

protected void repMessages_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
pnlMessages.Visible = false;
}
else if (e.Item.ItemType == ListItemType.Item)
{
pnlMessages.Visible = true;
}
}

(PS: I personally prefer PlaceHolder over Panel because PlaceHolder
doesn't have client side html footprint but Panel does render as <div>
which may disturb layout in some cases)
 
M

Muhammad Naveed Yaseen

1) In code-front make panels Visible property default to false (in
cases when repeater is not bound at all)

2) In code-front place a header template in repeater if it isn't
already there (even an empty one like <HeaderTemplate></
HeaderTemplate> would work)

3) In code-behind handle ItemDataBound event like following

protected void repMessages_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
pnlMessages.Visible = false;
}
else if (e.Item.ItemType == ListItemType.Item)
{
pnlMessages.Visible = true;
}
}


(PS: I personally prefer PlaceHolder over Panel because PlaceHolder
doesn't have client side html footprint but Panel does render as <div>
which may disturb layout in some cases)
 
R

Riki

Mick said:
Hi Folks,

I have a situation where I have a repeater within and ASP Panel on my
page. If the repeater contains no items, then I don't need to show
the panel, however if it does then I do.

I tried the following in the page load event:

// Check to see if we have any new messages
if(repMessages.Items.Count == 0)
{
pnlMessages.Visible = false;
}
if(repMessages.Items.Count > 0 )
{
pnlMessages.Visible = true;
}
However that didn't work, so I figured that the data must be bound
after the page load event fires.So I then tried:

protected void repMessages_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
pnlMessages.Visible = true;
}

Thinking that if an item is being databound, then the Items.Count
property of the repeater must be > 0. However that didn't work either.

So my question is, How can I show and hide a panel which contains a
repeater based on the content (or lack of) of the repeater itself?

Kind Regards
Mick

Just put your code in Repeater1_DataBound instead of Page_Load.
Don't use ItemDataBound, because it will be executed for every item.
Once (after the databinding) is enough.
 
R

Riki

Muhammad said:
Repeaters do not have DataBound event.

My mistake.

If you use declarative databinding with a SqlDataSourceControl or
AccessDataSourceControl, you may use the OnSelected event on that control.
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top