Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 622781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:01:17+00:00 2026-05-13T19:01:17+00:00

I recently read this article on smart use of ViewState and am particularly interested

  • 0

I recently read this article on smart use of ViewState and am particularly interested in not having unnecessary static data in ViewState. However, I’m also curious if I can do the same thing for a parent-child dropdown, like the classic Country/CountrySubDivision example.

So I have this markup:

    <asp:DropDownList runat="server" ID="ddlCountry" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
    <asp:DropDownList runat="server" ID="ddlCountrySubdivision" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
    <asp:Button runat="server" ID="btnTest" />

And this codebehind:

    public class Country
    {
        public string Name { get; set;}
        public int Id { get; set; }
    }

    public class CountrySubdivision
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public int CountryId { get; set; }
    }

    protected override void OnInit(EventArgs e)
    {
        var l = new List<Country>();
        l.Add(new Country { Name = "A", Id = 1 });
        l.Add(new Country { Name = "B", Id = 2 });
        l.Add(new Country { Name = "C", Id = 3 });
        ddlCountry.DataSource = l;
        ddlCountry.DataBind();

        var l2 = new List<CountrySubdivision>();
        l2.Add(new CountrySubdivision { Name = "A1", Id = 1, CountryId = 1 }); 
        l2.Add(new CountrySubdivision { Name = "A2", Id = 2, CountryId = 1 });
        l2.Add(new CountrySubdivision { Name = "B1", Id = 4, CountryId = 2 });
        l2.Add(new CountrySubdivision { Name = "B2", Id = 5, CountryId = 2 });
        l2.Add(new CountrySubdivision { Name = "C1", Id = 7, CountryId = 3 });
        l2.Add(new CountrySubdivision { Name = "C2", Id = 8, CountryId = 3 });

        // this does not work: always comes out 1 regardless of what's actually selected
        var selectedCountryId = string.IsNullOrEmpty(ddlCountry.SelectedValue) ? 1 : Int32.Parse(ddlCountry.SelectedValue);

        // this does work: 
        var selectedCountryIdFromFormValues = Request.Form["ddlCountry"];

        ddlCountrySubdivision.DataSource = l2.Where(x => x.CountryId == selectedCountryId).ToList();
        ddlCountrySubdivision.DataBind();

        base.OnInit(e);
    }

So the first thing I noticed is that even when EnableViewstate is false, my country control’s value is persisted across requests with no extra effort. Sweet. That’s a lot of serialized stuff I don’t need to send across the wire on form submissions.

Then I came to the example above with a pair of parent-child drop downs, and I see that ddlCountry.SelectedValue is defaulting whereas Request.Form["ddlCountry"] is reflecting the control’s value.

Is there a way to keep EnableViewState = "false" without resorting to Request.Form to get a dependent control’s value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:01:18+00:00Added an answer on May 13, 2026 at 7:01 pm

    Then I came to the example above with a pair of parent-child drop downs, and I see that ddlCountry.SelectedValue is defaulting whereas Request.Form[“ddlCountry”] is reflecting the control’s value.

    The reason you are seeing this behavior is that at that point in the page’s lifecycle, Viewstate hasn’t been loaded. When Viewstate is loaded, it is taken from the Request.Form object values, so you are seeing correct values there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`ve recently read this article about a user losing data on EC2 when dealing
I recently read this article related to indenting source files in Vim . However
I've recently read an article http://www.ravelrumba.com/blog/static-cookieless-domain/ about Serving Static Content from a Cookieless Domain
I recently read this article on ClientBundle and under the Levers and knobs section,
I recently read this article on GWT Lightweight Metrics and found it intriguing, but
I have recently read the safe bool idiom article. I had seen this technique
I recently read an old article it which this was suggested Avoid using the
I recently read this article . Which uses the Silverlight Bing maps control to
Recently I've read this article: http://www.smashingmagazine.com/2009/09/25/svn-strikes-back-a-serious-vulnerability-found/ Developers of many popular sites like apache.org, php.net
I've recently read this article on using printf and scanf in assembly: Meaning of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.