Custom control viewstate (?) issue!

U

u3k

I've been struggling for a day and yet haven't found a solution to
such a problem:

I have, let's say, an apsx page, which contains a UserControl, which
adds to it's Controls collection a new control, dynamically loaded via
LoadControl, which in order is also a UserControl, visually designed.
When I init this 'dynamic control', i set properties of this 'dynamic'
control's subscontrols (textboxes, labels - all WebControls), on first
load i see this initial, desired, values. But then, after a post back
- those initial value are lost. All viewstate stuff is set properly
thru the whole hierarcy of controls. The weird thing is that even if I
add any text to textboxes containig initial values in browser, e.g.
Initial value was 'Default', and i type up to the end of the string
'!!!!', after postback i get in textbox just '!!!!'.... Seems like
viewstate is restored but in some irrational way - only what is
received in postback! More: even if i will mix the parts of initial
value in browser text box, for example - switch part of the phrase via
cut and paste, theese parts also will be removed from viewstate. And
more: if i'll retype the exact string in browser textbox, the same
that was written in intialization part, but before that _clear_
(DEL-DEL-DEL:) textbox's contents, the string _typed_ is restored...
 
T

Teemu Keiski

Hi,

Do you add the control to Controls collection in Page_Init? If you do, you
need to set initial values on every request, because ViewState tracking
starts after Init.

Same concerns if you first set initial values when loading the control with
LoadControl but you haven't yet added the control to Controls collection.
Control lifecycle, which includes ViewState tracking etc, begins only after
control is added to the Controls collection.

TextBox returning the postback data, is normal, that is how it updates its
state. It updates the value in ViewState with the posted value. I've
explained some of the mechanics of it here:
http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx

Here's also threads at ASP.NEt Forums related to dynamical controls:
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=391019
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=285389
 
U

u3k

Teemu Keiski said:
Hi,

Do you add the control to Controls collection in Page_Init? If you do, you
need to set initial values on every request, because ViewState tracking
starts after Init.

Same concerns if you first set initial values when loading the control with
LoadControl but you haven't yet added the control to Controls collection.
Control lifecycle, which includes ViewState tracking etc, begins only after
control is added to the Controls collection.

Thanks! It made much clearer to me what happens for dynamic controls,
but... the problem want's there, i've discovered...

What happens: i have several objects placed on Session. After user
logs in, his objects (custom object taht contains user data,
Session["User"]) is put on Session. It lives in in intact just down to
logout. But there are objects that are refreshed each request - e.g.
Session["CurrentObject"], wich represents object whic corresponds a
node selected in website tree. Each requsest Session["CurrentObject"]
is replaced with new refernce to object. So, when in dynamic control I
init values with data from Session["User"]:

TextBox1.Text = ((Controller.ProxyObject)HttpContext.Current.Session["User"]).Login;

.... it all works fine!

but when i use (...HttpContext.Current.Session["CurrenObject"]).ObjectName,
in other words use refreshed object to initalize Text property, i get
behaivoir i've described. I've tried to catch wether control tries to
initialize property when with corrupt data, buti failed - data was
always available.

Weird! How can it be explained?

P.S. Thanks again for links - they had an emmense effect!
 
U

u3k

Thanks for your reply!
I've sent you an email (from: (e-mail address removed)) with description of my
problem
more detailed - i've found that it is a bit more cumbersum problem
that i've expected:)
I have an article coming up on MSDN about ASP.NET View State. It's not
yet published, but if you drop me your email address, I'd be happy to
send you a draft copy of the article. I think it would help clear up a
lot of misconceptions and strengthen your understanding of view state.
If you are interested, drop me a line at (e-mail address removed)

Happy Programming!

Thanks! I'd love to read it!!!
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top