Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
|
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
 
Old December 13th, 2003, 07:51 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default 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
 
Old December 13th, 2003, 08:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old December 13th, 2003, 09:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old December 13th, 2003, 10:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old December 13th, 2003, 11:06 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Good point, my bad for not recognizing that in the original post.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 13th, 2003, 01:34 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

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
 
Old December 13th, 2003, 06:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Data source control for Create User zoltac007 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 13 August 16th, 2006 02:34 PM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
Two data items from a populated dropdown menu bear1138 Dreamweaver (all versions) 1 August 13th, 2003 09:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.