Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Creating User Control


Message #1 by "Ganesh Ramamurthy" <r_ganesh76@h...> on Fri, 24 Jan 2003 04:28:28
Dear all,

I have created a usercontrol. in the class, I have created a property to 
store some values while the user enters values in a textbox within the 
user control. But, however, each time a server side code is executed in 
the form, the class of the usercontrol is getting initialized whereby the 
values which were originally stored is getting cleared. How can i prevent 
this from happening

Please help

Regards
Ganesh
Message #2 by "Arjen Oetomo" <arjeno@c...> on Mon, 27 Jan 2003 07:43:59
Just like an aspx page, a usercontrol has to maintain state between 
postbacks. 

You can store values in the viewState after a state change, for example:
    ViewState("myUserCtrlUserName") = userName

And on postback restore the values:
    userName = ViewState("myUserCtrlUserName")

Keep in mind that only serializable datatypes can be stored in the 
viewstate. Take a snapshot and store it in the ViewState and restore the 
state of your control after each postback.

I hope this helps.. 

Arjen O.


> Dear all,

> I have created a usercontrol. in the class, I have created a property to 
s> tore some values while the user enters values in a textbox within the 
u> ser control. But, however, each time a server side code is executed in 
t> he form, the class of the usercontrol is getting initialized whereby 
the 
v> alues which were originally stored is getting cleared. How can i 
prevent 
t> his from happening

> Please help

> Regards
G> anesh
Message #3 by "Rohit Arora" <rohit_arora@i...> on Mon, 27 Jan 2003 13:05:34 +0530
Hi Arjen,  

Can this be accomplished by "enableViewState=true"

peace
rohit

-----Original Message-----
From: Arjen Oetomo [mailto:arjeno@c...]
Sent: Monday, January 27, 2003 7:44 AM
To: ASP.NET
Subject: [aspx] Re: Creating User Control


Just like an aspx page, a usercontrol has to maintain state between 
postbacks. 

You can store values in the viewState after a state change, for example:
    ViewState("myUserCtrlUserName") = userName

And on postback restore the values:
    userName = ViewState("myUserCtrlUserName")

Keep in mind that only serializable datatypes can be stored in the 
viewstate. Take a snapshot and store it in the ViewState and restore the 
state of your control after each postback.

I hope this helps.. 

Arjen O.


> Dear all,

> I have created a usercontrol. in the class, I have created a property to 
s> tore some values while the user enters values in a textbox within the 
u> ser control. But, however, each time a server side code is executed in 
t> he form, the class of the usercontrol is getting initialized whereby 
the 
v> alues which were originally stored is getting cleared. How can i 
prevent 
t> his from happening

> Please help

> Regards
G> anesh
Message #4 by "Arjen Oetomo" <arjeno@c...> on Mon, 27 Jan 2003 10:23:24
Yes, enableViewState is set to true by default (on most Webcontrols) and 
saves all values in controls as textboxes, etc... But, it doesnt save 
custom properties on your userControl class.

As I understand he passes the value from the textbox to a property on his 
class.. In this case you need to save the properties state "manually" in 
the viewstate during postbacks. 
(or store the values in a hidden control with enableViewState=true)

Arjen O.

> Hi Arjen,  

Can this be accomplished by "enableViewState=true"

peace
rohit

-----Original Message-----
From: Arjen Oetomo [mailto:arjeno@c...]
Sent: Monday, January 27, 2003 7:44 AM
To: ASP.NET
Subject: [aspx] Re: Creating User Control


Just like an aspx page, a usercontrol has to maintain state between 
postbacks. 

You can store values in the viewState after a state change, for example:
    ViewState("myUserCtrlUserName") = userName

And on postback restore the values:
    userName = ViewState("myUserCtrlUserName")

Keep in mind that only serializable datatypes can be stored in the 
viewstate. Take a snapshot and store it in the ViewState and restore the 
state of your control after each postback.

I hope this helps.. 

Arjen O.


> Dear all,

> I have created a usercontrol. in the class, I have created a property to 
s> tore some values while the user enters values in a textbox within the 
u> ser control. But, however, each time a server side code is executed in 
t> he form, the class of the usercontrol is getting initialized whereby 
the 
v> alues which were originally stored is getting cleared. How can i 
prevent 
t> his from happening

> Please help

> Regards
G> anesh

  Return to Index