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 6004907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:43+00:00 2026-05-23T01:19:43+00:00

I’m trying to store a Dictionary<string, string> in the ViewState of a custom control

  • 0

I’m trying to store a Dictionary<string, string> in the ViewState of a custom control I’m developing for ASP.NET 2.0:

private Dictionary<String, String> Items
    {
        get
        {
            object d = ViewState["Items"];
            return (d == null ? null : (Dictionary<String, String>)d);
        }
        set
        {
            ViewState["Items"] = value;
        }
    }

Accessing it looks like this:

public void UpdateData
{
    if (this.Items == null)
        this.Items = new Dictionary<string, string>();
    else
        this.Items.Clear();
    //Fill the collection
}

When it gets set the first time the page loads, it appears to work fine. But on subsequent postbacks, the value returned is always null (the first condition always happens). Debugging shows that it’s getting null out of the ViewState in the property get.

I’ve done some research and have found that classes must implement IStateManager to be saveable in ViewState, and the Dictionary MSDN page appears to indicate that Dictionary<TKey, TValue> does not. But I’ve stored dictionaries before in ViewState without a problem. What’s going on here? Was my previous experience a fluke?

UPDATE: I tried adding some test code to the property: ViewState["ItemTest"] = "foo"; in the set and string test = (string)ViewState["ItemTest"]; in the get. Like the Dictionary, it comes out null. So it doesn’t appear to be a problem with the Dictionary being serializable. Also, to clarify, UpdateData is called from my RenderControl override, which happens after Page_Load in the page that contains the control.

  • 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-23T01:19:44+00:00Added an answer on May 23, 2026 at 1:19 am

    You can store the dictionary in ViewState, but you are attempting to do this too late in the page life cycle. Just as ViewState is loaded after Init, ViewState is saved before controls are rendered. Move your logic out of RenderControl and into another method or event handler earlier in the life cycle, such as PreRender.

    protected override void OnPreRender(EventArgs e)
    {
        if (this.Items == null)
        {
            this.Items = new Dictionary<string, string>();
        }
    
        base.OnPreRender(e);
    }
    

    You will notice the that object is no longer null on subsequent postbacks as long as ViewState is not being disabled on either the control or its parent.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.