Wednesday, March 21, 2012

DropdownList controls inside AccordionPane and Clearing ViewState on Control

I'm having problems with ViewState loading stale data on DropDowns embedded within an AccordionPane.

The dropdowns are bound to a generic collection object using Databinding via a 'PopulateList' method that's in a helper class:

//bind a listControl with an objectpublic static void PopulateList(System.Web.UI.WebControls.ListControl list,string valueDataName,string textDataName, Object bindingObject){ list.DataSource = bindingObject; list.DataValueField = valueDataName; list.DataTextField = textDataName; list.DataBind();}
the 'PopulateList' method gets called in the set property of each generic collection object: 

public System.Collections.Generic.IList<GenericCollectionItem<int,string>> PlanList

{get {return _planList; }set { _planList =value; ddPlan.Items.Clear();if (_planList.Count > 0) { ddPlan.Enabled =true; ListHelper.PopulateList(ddPlan,"ID","Description", _planList); }else { ddPlan.Enabled =false; } }}
The problem I'm having is when the list is empty, the dropdown retains the old values even
though the code to clear and disable the dropdown is being executed. Anyone know a way to
clear ViewState for an individual control or have any suggestions on what might be going on?
I don't want to disable the ViewState for the entire page or for the control, because the control
values will only be changed when the generic collection is changed, so i need viewstate for all other times...
Any help greatly appreciated!
Thanks

Hi kylen,

You could try Viewstate.Remove(key)

I think that the key is the ID of the control which viewstate is tracking. (more about viewstate here:http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx )

Another thing you could do is clear the items of the DDL in the load event. The Load occurs after the ViewState has set the last state-values to the controls (Well, I think the load is after ... otherwise try the PreRender of the page)

Let me know if anything worked out!

Kind regards,
Wim

No comments:

Post a Comment