Convert.FromBase64String() and ViewState

  • Thread starter frankswildyearstom
  • Start date
F

frankswildyearstom

Hello,



Q1 - If character ‘C’ is saved into viewstate, then just before the
page is rendered, Asp.Net serializes ‘C’ ( along with other data )
into Base64 string. If on postback I issue the following code:


In aspx page:

<%@ Page Language="C#" AutoEventWireup="true"
EnableViewStateMac="false".../>


In cs. file:

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "ABCDEF";
if (IsPostBack)
{
string viewStateString=Request["__VIEWSTATE"];

byte[] stringBytes = Convert.FromBase64String
(viewStateString);

for (int i = 0; i < stringBytes.Length; i++)
{
if(stringBytes == 67)
Label1.Text = ”deserialization successful”;
}
}
}


, then FromBase64String() should convert serialized data back into
their original format. Thus, one of ‘stringBytes’ fields should
contain a decimal value 67( which represents ASCII character C) .But
that doesn’t seem to be the case. Any ideas what I’m doing wrong?


Q2 - Since String represents a set of Unicode characters, I assume
that in the above example when “ABCDEF” is deserialized from the
viewstate, each character in “ABCDEF” string is represented with two
elements of a ‘stringBytes’ array?



Q3 - Is forms data, thus TextBox1.Text also saved in viewstate – I’m
asking this cos I don’t see a point in saving tha data since form will
save it automatically on postbacks.Are fields that are saved by
postback form automatically not included in viewstate

A) I would assume that postback data ( thus values of <input> controls
that are automatically saved on postbacks) is not also saved into
ViewState, since there wouldn't be any point in doing that

B) If so, then for example TextBox.Text doesn't use ViewState as its
backing store?


much appreciated



EDIT:

I must apologize for not mentioning that
 
F

frankswildyearstom

Note: you appear to be asking a question about ASP.NET.  Anything in your  
question that is specific to ASP.NET, you will get a much better reply to 
if you post in an appropriate forum, such as the ASP.NET newsgroup.
Not really – I realize out of ten Asp.Net related threads I may post
here, only one will get answered, but at least that one question will
get answered completely, since sometimes it takes four or more replies
for me to finally understand the subject at hand. Here people helping
tend to “persist”, while on other forums that is not the case most of
the time. Out of 20 or 30 threads I made on some other forum, only few
people managed to “stick” around long enough for me to finally grasp
it. So I got 20+ threads where there are still things about the
questions I posted I don’t quite understand.



That all depends on how the data is serialized in the first place.  If the  
string "ABCDEF" is serialized as UTF-16, then yes...you get two bytes for 
each character in the deserialized output.  If it's serialized as UTF-8,  
then that particular string will get you only one byte for each character 
(but could have two or more bytes for other characters).
“ABCDEF” isn’t serialized as either UTF-8 or UTF-16, but as Base64
encoding.

You keep mentioning this "viewstate".  If this is a standard part of  
ASP.NET, then this illustrates why you should post your question in a more  
appropriate forum.  If it's not, then you should be more specific about 
what "viewstate" is and how it gets initialized and used.

ViewState is Asp.Net specific
For not mentioning what?

I originally posted it in some other forum and mistakenly also copied
that part
 
J

Jesse Houwing

Hello (e-mail address removed),
Hello,

Q1 - If character ‘C’ is saved into viewstate, then just before the
page is rendered, Asp.Net serializes ‘C’ ( along with other data )
into Base64 string. If on postback I issue the following code:

In aspx page:

<%@ Page Language="C#" AutoEventWireup="true"
EnableViewStateMac="false".../>
In cs. file:

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "ABCDEF";
if (IsPostBack)
{
string viewStateString=Request["__VIEWSTATE"];
byte[] stringBytes = Convert.FromBase64String
(viewStateString);

for (int i = 0; i < stringBytes.Length; i++)
{
if(stringBytes == 67)
Label1.Text = â€deserialization successfulâ€;
}
}
}
, then FromBase64String() should convert serialized data back into
their original format. Thus, one of ‘stringBytes’ fields should
contain a decimal value 67( which represents ASCII character C) .But
that doesn’t seem to be the case. Any ideas what I’m doing wrong?


Why are you looking at the viewstate in this manner? You can use the debugger
to view exactly which values have been stored in the viewstate at runtime.
How and exactly which values are stored in what manner is for ASP.NET to
decide. You can use Reflector to see how the viewstate is being deserialized
internally by ASP.NET. Any value explicitly added to the viewstate will be
encoded into the page. Each control decides which values it needs in the
viewstate and why.
Q2 - Since String represents a set of Unicode characters, I assume
that in the above example when “ABCDEF†is deserialized from the
viewstate, each character in “ABCDEF†string is represented with two
elements of a ‘stringBytes’ array?

That depends on which serializer ASP.NET chooses. It can choose between a
binary serializer (for complex types), or a simple type serializer with special
support for strings, ints, doubles, arrays and such. Again, which serializer
it chooses and why is for ASP.NET to decide
Q3 - Is forms data, thus TextBox1.Text also saved in viewstate – I’m
asking this cos I don’t see a point in saving tha data since form will
save it automatically on postbacks.Are fields that are saved by
postback form automatically not included in viewstate

Yes, these values are stored in the viewstate, otherwise ValueChanged events
and such will not work, you'll need the previous values, and the current
values to see if anything changed.
A) I would assume that postback data ( thus values of <input> controls
that are automatically saved on postbacks) is not also saved into
ViewState, since there wouldn't be any point in doing that

Wrong, see above
B) If so, then for example TextBox.Text doesn't use ViewState as its
backing store?

Viewstate can be disabled for any ASP.NET control. That also disables some
functionality or requires you to do more work. With Viewstate enabled, the
value is copied just before rendering the control to the output, and it is
copied back to the control directly after restoring the control tree on your
page.

See this article for more information on the viewstate and on how to use it.

http://msdn.microsoft.com/en-us/library/ms972976.aspx (found by searching
"how does the asp.net viewstate work", first hit in google)

Next time, please post questions related to the ASP.NET runtime in the appropriate
groups.
 
F

frankswildyearstom

Hello (e-mail address removed),


Q1 - If character ‘C’ is saved into viewstate, then just before the
page is rendered, Asp.Net serializes ‘C’ ( along with other data )
into Base64 string. If on postback I issue the following code:
In aspx page:
<%@ Page Language="C#" AutoEventWireup="true"
EnableViewStateMac="false".../>
In cs. file:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "ABCDEF";
if (IsPostBack)
{
string viewStateString=Request["__VIEWSTATE"];
byte[] stringBytes = Convert.FromBase64String
(viewStateString);
for (int i = 0; i < stringBytes.Length; i++)
{
if(stringBytes == 67)
Label1.Text = ”deserialization successful”;
}
}
}
, then FromBase64String() should convert serialized data back into
their original format. Thus, one of ‘stringBytes’ fields should
contain a decimal value 67( which represents ASCII character C) .But
that doesn’t seem to be the case. Any ideas what I’m doing wrong?


Why are you looking at the viewstate in this manner?


I want to learn everything and I was following the example in a book
That depends on which serializer ASP.NET chooses. It can choose between a
binary serializer (for complex types), or a simple type serializer with special
support for strings, ints, doubles, arrays and such. Again, which serializer
it chooses and why is for ASP.NET to decide


Yes, these values are stored in the viewstate, otherwise ValueChanged events
and such will not work, you'll need the previous values, and the current
values to see if anything changed.


Wrong, see above


Viewstate can be disabled for any ASP.NET control. That also disables some
functionality or requires you to do more work. With Viewstate enabled, the
value is copied just before rendering the control to the output, and it is
copied back to the control directly after restoring the control tree on your
page.

See this article for more information on the viewstate and on how to use it.

http://msdn.microsoft.com/en-us/library/ms972976.aspx(found by searching
"how does the asp.net viewstate work", first hit in google)

Next time, please post questions related to the ASP.NET runtime in the appropriate
groups.

I don't know why the two of you are giving me the hard time. There
are numerous Asp.Net, Ado,Net and WinForm related questions being made
all the time here, but you don't go there telling people off.
So I'm asking why I'm being singled out ( perhaps it's just my
paranoia, but that's how I'm perceiving this )?

and I do apreciate your help
 
J

Jesse Houwing

Hello (e-mail address removed),
So I'm asking why I'm being singled out ( perhaps it's just my
paranoia, but that's how I'm perceiving this )?

and I do apreciate your help

It was a question I could formulate a good answer to, but I'm also following
the asp.net group, so I migth have noticed it there as well. No other reason.

The problem with questions in the wrong group is that usually there are less
poeple who are going to search there if they have a similar problem. And
more noise for people looking for specific answers for their problems related
to the C# language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top