User control property is blank

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

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
 
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
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>
 
Back
Top