Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

User control property is blank

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris McFarling - 21 Sep 2007 00:14 GMT
I'm trying to programatically set a private member variable within a user
control. In this case in the user control's Page_Load event, I set
m_PageTitle to equal "xxx". When I try to retieve that value from the
hosting page via a Get accessor (NavMenu1.PageTitle), the value is empty. Is
there anything wrong with this code?

control.ascx---------------------------------------------------
<%@ Control Language="VB" ClassName="navigation" %>
<script runat="server">
   Private m_PageTitle as String
   Public Property PageTitle As String
       Get
           Return m_PageTitle
       End Get
       Set
           m_PageTitle = Value
       End Set
   End Property

   Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
       m_PageTitle = "xxx"
   End Sub
</script>

page.aspx-----------------------------------------------------------
<%@ Register TagPrefix="PP" TagName="NavMenu" Src="~/control.ascx" %>

<html>
<body>
<script runat="server">

   Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
       Response.Write(NavMenu1.PageTitle)
   End Sub

</script>

<PP:NavMenu id="NavMenu1" runat="server" />
</body>
</html>
Just Me - 21 Sep 2007 00:22 GMT
The most likely cause is that you lost your variable during postback. You
need to persist the member variable using viewstate.

If you click a button on the web page, you will loose whatever private
member variables you have unless you store them. You can restore them on the
load event for the control and set the viewstate when you set the Property
of of the control.

'//On_Load
If  Postback then

   myVariableName = Ctype(viewstate("m_PageTitle"), string)

End If

'//Modified Property
Private m_PageTitle as String
   Public Property PageTitle As String
       Get
           Return m_PageTitle
       End Get
       Set
           m_PageTitle = Value
           viewstate( "MyVariableName") = m_PageTitle
       End Set
End Property

HTH

> I'm trying to programatically set a private member variable within a user
> control. In this case in the user control's Page_Load event, I set
[quoted text clipped - 36 lines]
> </body>
> </html>
bruce barker - 21 Sep 2007 00:32 GMT
probably the page_load's are not running in the order you expect.

-- bruce (sqlwork.com)

> I'm trying to programatically set a private member variable within a user
> control. In this case in the user control's Page_Load event, I set
[quoted text clipped - 36 lines]
> </body>
> </html>
Chris McFarling - 21 Sep 2007 00:43 GMT
Ahhhh, that is it. Using Page_LoadComplete() in the .aspx file solves it.

> probably the page_load's are not running in the order you expect.
>
[quoted text clipped - 40 lines]
>> </body>
>> </html>

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.