Wednesday, March 9, 2011

Keep Dropdown items attributes during async. postback

If you add custom attributes to ListItems such as color etc. the attributes are lost when a partial postback occurs. The attributes are not kept in ViewState.

Dropdown

One solution is to re-apply the styles when a partial postback occurs:

if (Page.IsPostBack)
{
    

if (ScriptManager.GetCurrent(this
.Page).IsInAsyncPostBack)
     {
        
foreach (ListItem Item in
Drp_Tasks.Items)
         {
            
if (Item.Value.Contains("WSS Task"
))
                 Item.Attributes.Add(
"Style", "color:blue"
);
            
else if (Item.Value.Contains("Timesheet Task"
))
                 Item.Attributes.Add(
"Style", "color:green");
         }
     } }


Another solution is to create a new ListItem class that keeps the attributes in ViewState.

No comments: