Confused by ViewState

M

msch-prv

This is a very elementary problem but it'is really confusing me.

To summarize, I have a simple textbox on a page with some data. After
post-back, the textbox contents show up fine. When I attempt to read
the textbox contents during the Pre-render event however, I get
nothing. (Note that the textbox is not embedded in any parent control
and page viewstate is enabled.)

Obviously, I'm missing something basic here. As far as I know,
Viewstate data should be available by render time. Thanks for any
pointers.

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Write("Info: " & Me.layProdCat.Text)
End sub

<div>
Info: <asp:textbox id="layProdCat" runat="server" />
</div>
 
S

Scott M.

Textboxes don't utilize ViewState in order to persist their data into the
next postback. You could change the EnableViewState property on your
textbox and it would still hold onto it's data when you submit the page (try
it).

Textboxes (and many other of the "standard" server controls [i.e. those
server controls that have an HTML counterpart]) simply post their data to
the server and then the server takes that value and makes it become the
default value for the postback.

Here's another test you can do. Submit your page with a value in the
textbox. When you get the postback page, view the source code from your
browser's View...Source option. You'll see that your textbox was rendered
with: value="whatever you entered the first time".

Now the other problem you are having is that you are attempting to get the
value of a control in an event that fires before the control is created.
Pre-render isn't the place to write your code. Put it in Page_Load,

-Scott
 
S

Scott M.

Just to add...

As far as I know, Viewstate data should be available by render time.

PreRender is not "render time" as you put it. It's before that. The
PreRenderComplete event fires when, well the PreRender stage is completed.
But, unless you have some specific reason for using these, Page_Load is
generally the place for page initialization code. And, it's a safe place to
do it because the controls have all been created by then.
 
M

msch-prv

Thanks Scott for your answers but I guess the issue may perhaps be
related to some DOM/ViewState interaction I don't grasp. Basically, I
am trying to preserve the style of a specific div block by saving its
layout state in a (hidden) textbox which is retrieved after each post-
back. The block can either be hidden or visible. As I mentioned, I am
able to correctly update textbox 'layProdCat' with the block's
current layout style (ie. display: none, etc.) whenever the block
display status changes.

The problem appears once the page is refreshed. Despite the fact that
the block's previous layout state shows up correctly in 'layProdCat,'
I cannot retrieve the textbox content (nothing). The html source code
shows that the textbox value is empty regardless of the block's
current state. Apparently when the page is refreshed, the div block is
re-generated with its default setting (visible). Now, the layprodCat
info must be located somewhere since it shows up on the page! How can
I retrieve it? I tried both at the load and pre-render stages to no
avail. Thanks for any advice.

PS: Hiding/displaying the block causes no change in the html source.
The layout block always contains the same layout directive. I guess
DOM is making the change on the fly. Does the ViewState look up the
state of DOM before posting back?
 
M

msch-prv

I finally got this resolved. For testing purposes, I had disabled a
series of controls that normally cause a post-back saving in the
process the page control values. A page refresh reveals which input
controls have lost their values under such a circumstance as IE
clearly shows. In FF however, page control values persist. Since I
basically do the chore of my development work in FF, I thought the
control values were available at the server level while they were in
effect retrieved from the browser's cache. Lesson learned! Thanks
again.
 
S

Scott M.

There is no DOM/ViewState interaction. DOM has is a client-side
implementation and ViewState can only be manipulated/accessed via a
server-side implementation. When ViewState exists at the client, it only
exists as a static HTML hidden form field.

If you want to persist the DIV, why not just make it a server control, by
adding runat="server" to the tag. Then, it should be persisted into the
postback and you wouldn't need a textbox at all?
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top