View State Issues

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

I am writing a complex data editor, and I am building a table with
other controls in it on the fly. ( I know, not for the faint of heart.
My other options are a quadruply nested data repeater, which is ugly
in its own right)
When the form first loads up, I populate a drop down list, set the
selected item to the first item, return my data for the selected item
in the drop down list, then build my table.
I am creating a few hidden controls in each row of the table to keep
track of some information.
I've been googling for days, and all of the viewstate articles I come
across usually deal with information not being persisted in
dynamically loaded controls.
However, my problem is just the opposite. Even though I set the value
of the hidden field in code everytime I rebuild the table, it
maintains the value that it had when the page first loaded up. I tried
setting the EnableViewState property for the hidden field to false
both before and after I add it to the controls collection to no avail.
The other interesting thing to note here is that my dynamically loaded
textboxes work fine, because when I load a different set of data, the
ID of the textbox changes, whereas the ID of the hidden fields stays
pretty much constant.
How can I prevent the value of my hidden fields from being overwritten
by the view state when I reload them?
Thanks!
 
I am writing a complex data editor, and I am building a table with
other controls in it on the fly. ( I know, not for the faint of heart.
My other options are a quadruply nested data repeater, which is ugly
in its own right)
When the form first loads up, I populate a drop down list, set the
selected item to the first item, return my data for the selected item
in the drop down list, then build my table.
I am creating a few hidden controls in each row of the table to keep
track of some information.
I've been googling for days, and all of the viewstate articles I come
across usually deal with information not being persisted in
dynamically loaded controls.
However, my problem is just the opposite. Even though I set the value
of the hidden field in code everytime I rebuild the table, it
maintains the value that it had when the page first loaded up. I tried
setting the EnableViewState property for the hidden field to false
both before and after I add it to the controls collection to no avail.
The other interesting thing to note here is that my dynamically loaded
textboxes work fine, because when I load a different set of data, the
ID of the textbox changes, whereas the ID of the hidden fields stays
pretty much constant.
How can I prevent the value of my hidden fields from being overwritten
by the view state when I reload them?
Thanks!

jeremy

I know what im proposing is rather ambiguous but hopefully it'll get
you pointed in the right direction.

When I run into viewstate problems they almost always revolve around
the page life cycle. Basically something is happening before or after
something else that im ignorant of and consequently I dont get the
results that I expect.

when you do anything in the ItemCreated event - this event will
actually fire before anything else happens - even the page load. This
is done to "preserve" the viewstate and write it back to the browser.

To be a little more specific - I wrote some fairly complicated paging
functionality for a datagrid. I was using viewstate and hidden fields
to preserve some values on pageload. This was a causing problems for
me because I was trying to check my viewstate in the pagload event and
kept getting the wrong values. I set a breakpoint in my ItemCreated
routine and saw it was getting called before pageload. Wth?? Thats
when i figured out it was a lifecyle issue -I needed to actually load
my hidden fields in the ItemCreated event - not the pagload - to get
everything to work correctly.

Hopefully my example didnt confuse you :) I'd compare your logic with
the page life cycle to make sure things are happening when they should
- chances are something's not happening in the order you need it to
and that's whats causing you problems.
 
Part of my problem here is a "chicken and egg" problem that I don't
know how to get around. If the drop down list has changed, then I need
to clear out and totally redo with a different set of data. If the
postback was caused by another reason (like the submit button which
will cause me to save the changed data to the database) then I need to
simply reload, preserving the values so I can read them in the button
process event.
Does that make sense?
 
Part of my problem here is a "chicken and egg" problem that I don't
know how to get around. If the drop down list has changed, then I need
to clear out and totally redo with a different set of data. If the
postback was caused by another reason (like the submit button which
will cause me to save the changed data to the database) then I need to
simply reload, preserving the values so I can read them in the button
process event.
Does that make sense?


I'd have to see the code to really understand whats going on but my
intial reaction is that you need to capture the values you need on
postback (in the button event) and then put them back into the fields
on the pageload or prerender event.

If you can provide a simplied version of the code you're running I'd
be happy to take a look and give some suggestions.
 
I've been doing alot of reading on ViewState, and I think the problem
is that I am setting the values too late in the cycle, since I am
dynamically loading all of my controls in the Page Load event.
One of the articles I read suggested loading the controls in the Init
event, but my problem is that I have to load data in response to the
selected value of the drop down list. In doing a quick experiment, the
SelectedIndex property of the drop down list in the Init event is
always -1, so I can't populate my data correctly.
The only thing I can think to do is redirect to myself when the drop
down list is changed passing in the data that I need through a query
string parameter, and that just seems horrible to me.
 
I've been doing alot of reading on ViewState, and I think the problem
is that I am setting the values too late in the cycle, since I am
dynamically loading all of my controls in the Page Load event.
One of the articles I read suggested loading the controls in the Init
event, but my problem is that I have to load data in response to the
selected value of the drop down list. In doing a quick experiment, the
SelectedIndex property of the drop down list in the Init event is
always -1, so I can't populate my data correctly.
The only thing I can think to do is redirect to myself when the drop
down list is changed passing in the data that I need through a query
string parameter, and that just seems horrible to me.


are you're binding all your data in the page load event?

instead of redirecting your page to itself (i imagine you're
suggesting that to reload the data you need by firing the pageload
event) move that specific code to it's own function (name it
'BindData' or something) and then call that function wherever you need
it - in the pageload and in the selectedindexchanged event of the
dropdownlist.

im just taking shots in the dark here - if you're still stumped dont
be afraid to post a code example for people to look at. Just be
careful of sharing data-sensitive material though =) dont wanna upset
"the man"
 
Back
Top