Accessing ViewState

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wASP

    Accessing ViewState


    Hi again,

    I'm having a problem accessing the ViewState object. I'm using the following
    two functions - as copied from the MS docs - and state.count is zero in the first
    - and the while loop in the second function never iterates:

    /* ------------------------------------------------------------------ */
    /* ------------------------------------------------------------------ */
    public string GetViewStateLis t ()
    {
    StateBag state = ViewState;
    string result = String.Empty;

    if (state.Count > 0)
    {
    int upperBound = state.Count;
    string[] keys = new string[upperBound];
    StateItem[] values = new StateItem[upperBound];
    state.Keys.Copy To(keys, 0);
    state.Values.Co pyTo(values, 0);
    StringBuilder options = new StringBuilder() ;

    for(int i = 0; i < upperBound; i++)
    {
    result = result + "<br>keys:" + keys[i] + "::::values[i].Value:" +
    values[i].Value;
    }

    return result;
    }
    return "[no count value]";
    }
    /* ------------------------------------------------------------------ */
    /* ------------------------------------------------------------------ */
    public string EnumerateViewSt ate()
    {
    Int32 ndx = 0;
    string keyName, keyValue;
    string result = String.Empty;
    StateItem myStateItem;
    IDictionaryEnum erator myDictionaryEnu merator = ViewState.GetEn umerator ();

    result += "[start]> ";

    while(myDiction aryEnumerator.M oveNext())
    {
    ndx++;
    result = result + "[" + ndx.ToString() + "]";
    keyName = (string)myDicti onaryEnumerator .Key;
    myStateItem = (StateItem)myDi ctionaryEnumera tor.Value;
    keyValue = (string)myState Item.Value;
    result = result + "<br>ViewSt ate[" + keyName + "] = " + keyValue;
    }
    return result;
    }
    /* ------------------------------------------------------------------ */
    /* ------------------------------------------------------------------ */

    Does anyone have any ideas?

    THANKS!!!

    - wASP
  • shadow.demon@gmail.com

    #2
    Re: Accessing ViewState

    Hey dude,
    I'm not sure if i'm on the right track with you here, like your trying
    to cycle the viewstate for your object. I used to use:

    string strUserID = (string)ViewSta te["UserID"];

    in asp.net 1.1 not sure if they're doing it different now. Then i'd do
    a check to make sure it wasn't null or empty or something.

    Hope this helps,
    -Mark

    Comment

    • Alvin Bruney [Microsoft MVP]

      #3
      Re: Accessing ViewState

      you get to viewstate using viewstate["item"]. The statebag is for backwards
      compatibility

      --
      Regards
      Alvin Bruney
      [Shameless Author Plug]
      The Microsoft Office Web Components Black Book with .NET
      available at www.lulu.com/owc, Amazon, B&H etc
      -------------------------------------------------------------------------------


      "wASP" <wylbur[at]ev1[dot]net> wrote in message
      news:vuute19nm1 85dei6tdlgv4j6t 83bd3lsf4@4ax.c om...[color=blue]
      >
      > Hi again,
      >
      > I'm having a problem accessing the ViewState object. I'm using the
      > following
      > two functions - as copied from the MS docs - and state.count is zero in
      > the first
      > - and the while loop in the second function never iterates:
      >
      > /* ------------------------------------------------------------------ */
      > /* ------------------------------------------------------------------ */
      > public string GetViewStateLis t ()
      > {
      > StateBag state = ViewState;
      > string result = String.Empty;
      >
      > if (state.Count > 0)
      > {
      > int upperBound = state.Count;
      > string[] keys = new string[upperBound];
      > StateItem[] values = new StateItem[upperBound];
      > state.Keys.Copy To(keys, 0);
      > state.Values.Co pyTo(values, 0);
      > StringBuilder options = new StringBuilder() ;
      >
      > for(int i = 0; i < upperBound; i++)
      > {
      > result = result + "<br>keys:" + keys[i] + "::::values[i].Value:"
      > +
      > values[i].Value;
      > }
      >
      > return result;
      > }
      > return "[no count value]";
      > }
      > /* ------------------------------------------------------------------ */
      > /* ------------------------------------------------------------------ */
      > public string EnumerateViewSt ate()
      > {
      > Int32 ndx = 0;
      > string keyName, keyValue;
      > string result = String.Empty;
      > StateItem myStateItem;
      > IDictionaryEnum erator myDictionaryEnu merator = ViewState.GetEn umerator
      > ();
      >
      > result += "[start]> ";
      >
      > while(myDiction aryEnumerator.M oveNext())
      > {
      > ndx++;
      > result = result + "[" + ndx.ToString() + "]";
      > keyName = (string)myDicti onaryEnumerator .Key;
      > myStateItem = (StateItem)myDi ctionaryEnumera tor.Value;
      > keyValue = (string)myState Item.Value;
      > result = result + "<br>ViewSt ate[" + keyName + "] = " + keyValue;
      > }
      > return result;
      > }
      > /* ------------------------------------------------------------------ */
      > /* ------------------------------------------------------------------ */
      >
      > Does anyone have any ideas?
      >
      > THANKS!!!
      >
      > - wASP[/color]


      Comment

      • wASP

        #4
        Re: Accessing ViewState

        On 2 Aug 2005 02:53:16 -0700, shadow.demon@gm ail.com wrote:
        [color=blue]
        >Hey dude,
        >I'm not sure if i'm on the right track with you here, like your trying
        >to cycle the viewstate for your object. I used to use:
        >
        >string strUserID = (string)ViewSta te["UserID"];
        >
        >in asp.net 1.1 not sure if they're doing it different now. Then i'd do
        >a check to make sure it wasn't null or empty or something.
        >
        >Hope this helps,
        >-Mark[/color]

        Hi Shadow Demon,

        Thanks for the response, but it doesn't really address my issue.

        I need to be able to iterate through all entries to the ViewState.

        Apparently, noone has any idea as to what the problem is.

        Thanks again,

        - wASP

        Comment

        • wASP

          #5
          Re: Accessing ViewState

          On Tue, 2 Aug 2005 13:13:47 -0400, "Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc>
          wrote:
          [color=blue]
          >you get to viewstate using viewstate["item"]. The statebag is for backwards
          >compatibilit y[/color]

          Thanks Alvin,

          So how would I get an array of strings that enumerates all
          of the available/valid indicies within the viewstate object?

          - wASP

          Comment

          • Alvin Bruney [Microsoft MVP]

            #6
            Re: Accessing ViewState

            you can't. if you need this for reporting, just turn on tracing and
            viewstate will be dumped on the page. tracing internally enumerates the
            viewstate object. i'm not aware of a way to enumerate the viewstate object
            otherwise.

            --
            Regards
            Alvin Bruney
            [Shameless Author Plug]
            The Microsoft Office Web Components Black Book with .NET
            available at www.lulu.com/owc, Amazon, B&H etc
            -------------------------------------------------------------------------------


            "wASP" <wylbur[at]ev1[dot]net> wrote in message
            news:maive1151g cul3vmd4giq6u92 08m7khah8@4ax.c om...[color=blue]
            > On Tue, 2 Aug 2005 13:13:47 -0400, "Alvin Bruney [Microsoft MVP]"
            > <www.lulu.com/owc>
            > wrote:
            >[color=green]
            >>you get to viewstate using viewstate["item"]. The statebag is for
            >>backwards
            >>compatibili ty[/color]
            >
            > Thanks Alvin,
            >
            > So how would I get an array of strings that enumerates all
            > of the available/valid indicies within the viewstate object?
            >
            > - wASP[/color]


            Comment

            • wASP

              #7
              Re: Accessing ViewState

              On Wed, 3 Aug 2005 22:56:41 -0400, "Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc>
              wrote:
              [color=blue]
              >you can't. if you need this for reporting, just turn on tracing and
              >viewstate will be dumped on the page. tracing internally enumerates the
              >viewstate object. i'm not aware of a way to enumerate the viewstate object
              >otherwise.[/color]

              Ahhh - I guess that explains why I can't do it then.

              THANKS Alvin!

              - wASP

              Comment

              Working...