!!!Attention ASP .Net Gurus :: Repeater problem!!!


J

John

Hi all,

I have a Repeater problem where the code within the <ItemTemplate> and
<AlternatingItemTemplate> tags are not being executed.

<asp:Repeater ID="myRepeater" Runat="server">
<HeaderTemplate>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td ID="tdMyHeader" Runat="server">
...
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
...
<td><%# DataBinder.Eval(Container.DataItem,
"MyDateTime", "{0:h:mmtt}") %></td>
...
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
...
<td><%# DataBinder.Eval(Container.DataItem,
"MyDateTime", "{0:h:mmtt}") %></td>
...
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

The DataSource property on the Repeater is populated with a DataView
containing two records.

....
using (DataView dv = MyDataView())
{
if (dv != null && dv.Count > 0)
{
myRepeater.DataSource = dv;
}
...
}

myRepeater.DataBind();
....

The ItemDataBound event on the Repeater is being set to the
myRepeater_ItemDataBound and successfully executes once the DataBind
method is called.

protected void myRepeater_ItemDataBound(object source,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
...
HtmlTableCell tdMyHeader =
(HtmlTableCell)e.Item.FindControl("tdMyHeader"); // Code stopped on
breakpoint here.
...
}

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
bool didTheCodeStopHere = false; // Code DID NOT stop on
breakpoint here!?!
}

if (e.Item.ItemType == ListItemType.Footer)
{
bool didTheCodeStopHere = true; // Code stopped on breakpoint
here.
}
}

As the above comments state, the code stopped on the Header and Footer
breakpoints however did not stop on the Item/AlternatingItem
breakpoint. Also, when the code stopped on the header and footer
breakpoints I checked the ((DataView)source.DataSource).Count property
and it still contained two records.

Has anyone experience anything similar?

Thanks in advance,

J
 
Ad

Advertisements

S

Shimon Sim

Try to set write it this way

((e.Item.ItemType == ListItemType.Item )|| (e.Item.ItemType ==
ListItemType.AlternatingItem))
 
Ad

Advertisements

J

John

Thanks Shimon,

However, I found it!

It was the 'using' statement that defines a scope at the end of which
an object will be disposed. Translated - this emptied the DataView
before the data could be accessed from the ItemDataBound Event.

To resolve, change this:

....
using (DataView dv = MyDataView())
{
if (dv != null && dv.Count > 0)
{
myRepeater.DataSource = dv;
}
...
}

rpt.DataBind();
....

to this:

....
DataView dv = MyDataView();
if (dv != null && dv.Count > 0)
{
rpt.DataSource = dv;
}
....

rpt.DataBind();
....

Regards,

J
 

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

Top