Groups | Blog | Home
all groups > asp.net building controls > september 2003 >

asp.net building controls : Should I use the PreRender event



STom
9/28/2003 7:06:10 AM
I have several user controls where I am not using viewstate.

In the OnLoad, I call functions that will take the data out of the form
fields and save the data into a session object. Then I display what is in
the session object when the control is displayed to the client.

I have been reading about the control execution lifecycle and it seems that
PreRender might be of better use than the OnLoad event.

For example, if in the OnLoad event, I call SaveSessionData which does
something like:
If Not (Request.Form(Me.txtFirstName.UniqueID)) Is Nothing Then
Session("Person").FirstName =
Request.Form(Me.txtFirstName.UniqueID)
End If

and then call GetSessionData which does:
Me.txtFirstName.Text = Session("Person").FirstName

Is it not better to make the GetSessionData call in the prerender event?

BTW, the reason I have two separate functions to save and get the session
data is because in GetSessionData there are some other things I have to do
to set dropdowns etc.

Thanks.

STom

Oleg Ogurok
9/29/2003 4:37:39 PM

Instead of using this approach and peeking into Request.Form() directly, you
should attach event handlers to your controls and work with the posted
values there.
For example, you can attach an event handler for txtFirstName.TextChanged.
In this case, at postback you will have the newly entered text available as
txtFirstName.Text, but only inside your event handler, not in Page_Load().
And you don't need to use Page_PreRender.

-Oleg.

[quoted text, click to view]

AddThis Social Bookmark Button