locked
Trying to Achieve Unique State for Each of Many Instantiated User Controls RRS feed

  • Question

  • User-1334042570 posted

    Imagine having a web page in which you instantiate multiple copies of the same user control, which we'll call "Rectangle".  On the code page of Rectangle is this property:

        public bool Initialized
        {
          get
          {
            return Convert.ToBoolean(this.ViewState["Initialized"]);
          }
    
          set
          {
            this.ViewState["Initialized"] = value.ToString();
          }
        }
    

    I had thought that each instantiated copy of Rectangle added to the web page maintained its own unique copy of the "Initialized" ViewState variable.  But testing has revealed that this doesn't seem to be the case.

    Is it, in fact the case, that the ViewState object is unique to the web page but not to each user control?  If so, then what is the best practice approach to keep track of a unique variable for each instantiation of a user control?

    Robert



    Friday, May 3, 2013 11:30 PM

Answers

  • User-1334042570 posted

    Thanks for this.  However, further testing revealed that each instantiated User Control maintained its own ViewState properties.  So ViewState["Initialized"] for instantiated User Control #1 is unique to that object.  Such is the same for instantitated User Control #2, #3, and so on.

    Where I was going wrong was that my restoration of the previously instantiated user controls was not always identical to the order they were instantiated prior to the PostBack.  So, for example, when they were initially added to my web page, they might have been in this order: 1,2,3,4,5,6 but then after a PostBack they might have been added in this order: 1,2,3,5,6,4   There was a good reason for this but I realized that I had to improve my algorithm to ensure that the reinstantiation (to invent a new word!) was identical to what was done originally.

    It all works well now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 6, 2013 12:13 PM

All replies

  • User-1241139641 posted

    You can use ControlState for this.

    http://haacked.com/archive/2007/03/15/gain-control-of-your-control-state.aspx

    Monday, May 6, 2013 12:02 PM
  • User-1334042570 posted

    Thanks for this.  However, further testing revealed that each instantiated User Control maintained its own ViewState properties.  So ViewState["Initialized"] for instantiated User Control #1 is unique to that object.  Such is the same for instantitated User Control #2, #3, and so on.

    Where I was going wrong was that my restoration of the previously instantiated user controls was not always identical to the order they were instantiated prior to the PostBack.  So, for example, when they were initially added to my web page, they might have been in this order: 1,2,3,4,5,6 but then after a PostBack they might have been added in this order: 1,2,3,5,6,4   There was a good reason for this but I realized that I had to improve my algorithm to ensure that the reinstantiation (to invent a new word!) was identical to what was done originally.

    It all works well now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 6, 2013 12:13 PM
  • User-1241139641 posted

    Ok. Good to know. Laughing

    Monday, May 6, 2013 1:14 PM