Thursday, June 2, 2016

how to set a message for when there are no records at an old school web forms Repeater to unsure the header isn't just floating there by itself

I found something at StackOverflow suggesting that you should set the OnItemDataBound method for your Repeater like so:

public void BonesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (BonesRepeater.Items.Count < 1)
   {
      if (e.Item.ItemType == ListItemType.Header)
      {
         HtmlGenericControl foo = (e.Item.FindControl("Foo") as HtmlGenericControl);
         foo.Visible = true;
      }
   }
}

 
 

I had better luck with something like this:

public void BonesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (BonesRepeater.Items.Count < 1)
   {
      if (e.Item.ItemType == ListItemType.Header)
      {
         e.Item.Controls[1].Visible = true;
      }
   }
}

 
 

The control, would be something put inside of the HeaderTemplate tags in the Repeater like so:

<div id="Foo" runat="server" visible="false">
   There are no bones in ice cream!
</div>

No comments:

Post a Comment