Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 13th, 2003, 07:51 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cairo, , Egypt.
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
  #2 (permalink)  
Old December 13th, 2003, 08:00 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
  #3 (permalink)  
Old December 13th, 2003, 09:42 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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.
  #4 (permalink)  
Old December 13th, 2003, 10:50 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
  #5 (permalink)  
Old December 13th, 2003, 11:06 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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.
  #6 (permalink)  
Old December 13th, 2003, 01:34 PM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cairo, , Egypt.
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
  #7 (permalink)  
Old December 13th, 2003, 06:29 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.