Accessing ViewState

  • Thread starter Thread starter wASP
  • Start date Start date
W

wASP

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 GetViewStateList ()
{
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.CopyTo(keys, 0);
state.Values.CopyTo(values, 0);
StringBuilder options = new StringBuilder();

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

return result;
}
return "[no count value]";
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
public string EnumerateViewState()
{
Int32 ndx = 0;
string keyName, keyValue;
string result = String.Empty;
StateItem myStateItem;
IDictionaryEnumerator myDictionaryEnumerator = ViewState.GetEnumerator ();

result += "[start]> ";

while(myDictionaryEnumerator.MoveNext())
{
ndx++;
result = result + "[" + ndx.ToString() + "]";
keyName = (string)myDictionaryEnumerator.Key;
myStateItem = (StateItem)myDictionaryEnumerator.Value;
keyValue = (string)myStateItem.Value;
result = result + "<br>ViewState[" + keyName + "] = " + keyValue;
}
return result;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

Does anyone have any ideas?

THANKS!!!

- wASP
 
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)ViewState["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
 
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
 
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)ViewState["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

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
 
you get to viewstate using viewstate["item"]. The statebag is for backwards
compatibility

Thanks Alvin,

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

- wASP
 
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 said:
you get to viewstate using viewstate["item"]. The statebag is for
backwards
compatibility

Thanks Alvin,

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

- wASP
 
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.

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

THANKS Alvin!

- wASP
 
Back
Top