Conditional logic inside repeaters
November 9, 2006
Today I was trying to make some asp:Labels visible or not based on what kind of data the repeater was showing, ie having som kind of if-else-clause to decide what to show.
That wasn’t as easy as I thought, and I saw that many people had had the same problem when googling this question at Google Groups.
The solution for me was to add an OnItemDataBound property to the repeater, like this:
And then, in the codebehind file I added an event handler.
protected void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
Inside that event handler I simply added the conditional logic for showing or not showing the stuff I wanted to.
visibility = true;
visibility = false;
((Label)e.Item.FindControl("lblLabelName")).Visible = visibility;
That did the trick for me, hope it helps you.