 |
| ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 13th, 2003, 07:51 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to keep data first populated to user control
i have made a user control and put it in Aspx wep page, so when the page_load event of the web page happen i populate the user control public variables under condition it's the first time to load the page by checking:
If Not Page.IsPostBack Then
'populating the user control public variables
end if
but when any request to the srver happen the page loads again and the user control public variable is empty:(
any one have a sol. to pass this problem
Ahmed Ali
Software Developer
__________________
Ahmed Ali
Senior Software Developer
|
|

December 13th, 2003, 08:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
How are you implementing your "public variables"? If all you have are simple variables, their values do get lost. When the page posts back, controls get their values from ViewState. However, your variables don't use this, so they won't keep their value.
You could something like ViewState["MyProperty"] = MyProperty to store that data. On PostBack of the control, check the ViewState for each property and restore its value.
Alternatively, depending on the purpose of the control, you could assign the values of the public properties for the control on PostBack of the page as well.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

December 13th, 2003, 09:42 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
The best way I've found to implement this is to use the public variable on the control to directly expose the control field you need.
Public Property MyTextBox_Text() As String
Get
Return MyTextBox.Text
End Get
Set(value As String)
MyTextBox.Text = value
End Set
End Property
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

December 13th, 2003, 10:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yeah, that would work well, but only for properties that directly map to a Server Control nested in the User Control.
If you're trying to maintain state of your own properties, you'll need to use ViewState or some other state mechanism.
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

December 13th, 2003, 11:06 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Good point, my bad for not recognizing that in the original post.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

December 13th, 2003, 01:34 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thx Imar,Peter
from your views that i cant do what i need else storing in one property of the control:(
Ahmed Ali
Software Developer
|
|

December 13th, 2003, 06:29 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
You can do what you want to do. If your goal is to be able to make this call during one page hit (postback or first time)...
ucMyUserControl.MyProperty = "..."
Then you want to make this call in a following postback...
something = ucMyUserControl.MyProperty
you must use Imar's suggestion. You need to manually handle storing that value in the page's viewstate so that the value can be restored.
You need to store those values when something happens on the page that changes a value. So in your user control public property accessors you can save the value to ViewState:
Public Property Property1() As String
Get
Return m_Property1InternalVar
End Get
Set(ByVal Value As String)
m_Property1InternalVar = Value
ViewState("Property1InternalVar") = m_Property1InternalVar
End Set
End Property
'Repeat for all properties
In page_load, conditionally restore the property value(s):
Private Sub Page_Load(...) Handles MyBase.Load
...
If IsPostback Then
m_Property1InternalVar = ViewState("Property1InternalVar")
'Repeat for all internal variables
End If
End Sub
Peter
------------------------------------------------------
Work smarter, not harder.
|
|
 |