views:

121

answers:

1

I'm binding to a dropdown. It works on the initial load. On subsequent loads (postbacks) it doesn't refresh the items in the dropdown.

using (DataView dv = dtProductGroup.DefaultView)
{
        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
}

I've confirmed that on the postbacks the data is being bound to the dropdown and items are successfully added. But when the page renders the dropdown doesn't have any of the new values.

I see two possibilities: The control isn't rendering the new values or the values are being cleared. I'm at a loss of where to look for possible problems.

Edit

I discovered the problem. The dropdownlist was embedded in an Conditional UpdatePanel. Simply calling "UpdatePanel.Update();" solved the problem.

+1  A: 

Upon Postback the viewstate is being reapplied + you said you're trying to load values again. I'd suggest letting viewstate carry all the weight on postback. Only load the values when the page is first hit by adding if (! IsPostBack) like so

using (DataView dv = dtProductGroup.DefaultView)
{
   if (! IsPostBack) {

        dv.ApplyDefaultSort = false; 
        dv.Sort = "KVIGroupName ASC";

        ddlGroup.ClearSelection();
        ddlGroup.Items.Clear();

        string strAll = Localization.GetResourceValue("_strddlStatusLBAll");
        ddlGroup.DataValueField = "KVIGroupId";
        ddlGroup.DataTextField = "KVIGroupName";
        ddlGroup.DataSource = dv;
        ddlGroup.DataBind();

        ListItem item = new ListItem(strAll, "0");
        ddlGroup.Items.Insert(0, item); 
   }
}

Edit: Also, your syntax ensures the DataView object referenced by dv is Disposed of when the code block exits. My second guess is this causes a side-effect that causes the problem.

using (DataView dv = dtProductGroup.DefaultView)
{

Instead leave out the using and write a regular declaratoin like the following (The DataView is going to be Disposed along with everything else when the page is done rendering so there's not really any need to do it yourself).

DataView dv = dtProductGroup.DefaultView;

See the MSDN documentation about 'using' and IDisposable for detailed info.

John K
+1 thanks for the through answer
Chuck Conway