User control property is blank

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
 
B

bruce barker

probably the page_load's are not running in the order you expect.


-- bruce (sqlwork.com)
 
C

Chris McFarling

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Master Page properties 4
Control Property. 3
Property. Control or View State? 1
Property 1
Property 1
Page level (user defined) properties? 1
Custom web control string 2
Problem with control and ViewState 3

Top