By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Accessing ViewState

P: n/a

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[i] + "::::values[i].Value:" +
values[i].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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
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:vu********************************@4ax.com...

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[i] + "::::values[i].Value:"
+
values[i].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

Nov 17 '05 #3

P: n/a
On 2 Aug 2005 02:53:16 -0700, sh**********@gmail.com wrote:
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
Nov 17 '05 #4

P: n/a
On Tue, 2 Aug 2005 13:13:47 -0400, "Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc>
wrote:
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
Nov 17 '05 #5

P: n/a
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:ma********************************@4ax.com...
On Tue, 2 Aug 2005 13:13:47 -0400, "Alvin Bruney [Microsoft MVP]"
<www.lulu.com/owc>
wrote:
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

Nov 17 '05 #6

P: n/a
On Wed, 3 Aug 2005 22:56:41 -0400, "Alvin Bruney [Microsoft MVP]" <www.lulu.com/owc>
wrote:
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
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.