2
votes

I'm working on ASP.NET 4.0 and I'm having problems for saving the selected value in a DropDownList.

I initialize it in the Web Control's PreRender method as follows:

    if (!Page.IsPostBack)
        LoadCountryList();

Where LoadCountryList's code is:

private void LoadCountryList()
{
    var vr = new CountryRepository();
    var countryList = vr.GetAllForList();

    DdlCountry.EnableViewState = true;
    DdlCountry.DataValueField = "code";
    DdlCountry.DataTextField = "name";
    DdlCountry.DataSource = countryList;
    DdlCountry.DataBind();
}

After I submit the form, I've noticed the DropDownList is empty(it's not before postback). If you take a look at my code, I've enabled the ViewState for this control, but I'm not getting any results yet. Does anybody know what's going on?

EDIT:

Here is the simple code of the control in the aspx file:

                <asp:DropDownList Runat="server" ID="DdlCountry">
                </asp:DropDownList>
4
is the DDL dynamically added, or declarative in the ASPX?x0n
try putting enableviewstate="true" in the markup instead of code. just a hunch, i could be wrong... (i'm assuming you've got enableviewstate="false" in the @page directive, right?)x0n
Where? I don't know where to look.Brian Roisentul
@Brian: Triple check that there is no code reseting the dropdown and you're not aware of itClaudio Redi
There's not, because I've created it.Brian Roisentul

4 Answers

9
votes

If it's in prerender, then that's before ViewState is initialized, so you're not reloading the data on a postback and you're also not able to get from ViewState because it's not put in there. The best thing with dropdowns is to load 'em up even on postback, but with some caching involved. That way your ViewState isn't bloated, it still handles your event on postback, and you're not hammering your database.

UPDATE:

Correction. @StriplingWarrior is correct in the comment below about PreRender (it will load into ViewState when in PreRender). However, I don't see how you can get simpler than my suggestion. For dropdowns, do the following:

  1. Load data in control/page init (do not check the IsPostBack flag) instead of PreRender.
  2. Done.

However, you can improve this by making sure your data is cached (as it will now be hit every time your page/control init is run), but unless it's high traffic, this isn't overly concerning..or at least, it needs to be evaluated along with all other caching/database concerns.

The biggest reason this is a way better solution in general (aside from fixing your issue), is that your ViewState doesn't get bloated unnecessarily with all your items (Base64-encoded to boot) while still being able to track the selected item in the dropdown (turning off ViewState for dropdowns makes it so you can't track your selected item without having to resort to viewing the form post value directly).

7
votes

I observed this phenomenon if EnableViewState of the MasterPage is set to false. (VS2008)

My solution was to fill the dropdown both in GET and in POST, and to retrieve the SelectedValue from the Form variables in Page_Load:

if (IsPostBack)
{
    dropdown.SelectedValue = Request.Form[dropdown.UniqueID];
}
0
votes

The ViewState and ASP.NET Page Lifecycle are strange beasts. You sometimes have to get just the right "timing." Try:

  • Performing this work in Page_Load instead of PreRender. Just to humor me.
  • Enabling viewstate at an earlier time, like on Init, and regardless of whether the page is a postback. It needs to be enabled when you hit the InitComplete event in order to Load the previous viewstate results on a postback.

Good luck.

0
votes

Solution:

Set Enable View State=true