Sunday, February 20, 2011

Hide / Show Datalist item in datalist asp.net c#

Hello everyone -
Its time again for a new blog post.
This time it will be related to asp.net datalist and datalistitems.
I was asked to hide show datalistitems in a datalist control depending from some parameters in the code.
Normally I did a loop through all the datalistitems in the datalist and if something is as it should be, enable that listitem.

foreach (DataListItem item in this.dlShopping.Items)
{
string namee = this.dlShopping.DataKeys[item.ItemIndex].ToString();
item.Visible = false;
if (namee.Equals("UPS GROUND") || namee.Equals("UPS 2 DAY AIR"))
{
item.Visible = true;
}
}

The code was working fine, but the end result was that all the datalist items were displayed, without filtering the one which were suppose to show up.

The solution to really filter the results was to add one more line in the code about the layout of the datalist.
dlShopping.RepeatLayout = RepeatLayout.Flow;

This helps the items to be displayed in random structure without using a table.
So, next time you want to do something like I did today, that line of code will solve your problems.
Thanks for reading.

No comments:

Post a Comment