Filed under C# , General
One of the annoying little things about the asp.net repeater is that if you have text in a <HeaderTemplate> this will be shown on screen even if the repeater is empty. Nine times out of ten you will actually want to hide the text and only show it if the repeater contains information. This can be done by doing the following:
First adjust your repeater so that on PreRender of the repeater you call some code from your code behind:
Header text inside my repeater
... show database values in here ...
Next on your code behind page put in the following to check if the repeater contains any values:
protected void MyRepeater_PreRender(object sender, System.EventArgs e)
{
//check to see if the repeater contains any items and if not hide it
if (MyRepeater.Items.Count == 0)
{
MyRepeater.Visible = false;
}
}
Fire up your page and test the repeater with and without items inside it. You should notice that when the repeater is empty the header text is not displayed which is what we wanted to happen.
fed8aec7-9b5b-4794-8d09-2300902e15bd|3|2.7