Sharing variables / properties between User Controls and Masterpages in ASP.Net

Hi There

Fairly new to ASP.net. I have a masterpage with several user controls embedded in it. Please could someone explain how to (or why I cant) share variables or properties between the master page and the user control.

Here is what I was trying:

The master page .vb file:


Imports System.Data
Imports System.Data.SqlClient

Partial Class Site
    Inherits System.Web.UI.MasterPage

    Dim content_idno As Int32 = 0

    Private _breadcrumbs As String
    Public Property breadcrumbs() As String
        Get
            Return _breadcrumbs
        End Get
        Set(ByVal value As String)
            _breadcrumbs = value
        End Set
    End Property



In the user control I have tried things like:
Me.Page.Master.breadcrumbs

and

Dim _masterpage As MasterPage = CType(Me.Page.Master, MasterPage)
_masterpage.breadcrumbs


But nothing seems to work I can access other controls in the masterpage from the user control using:
Dim RightCol As PlaceHolder
        RightCol = CType(Me.Page.Master.FindControl("RightCol"), PlaceHolder)


How do I access the public property?

Thanks.
jazzer102Asked:
Who is Participating?
 
chrisrbloomConnect With a Mentor Commented:
try this:

Content1.breadcrumbs = "some text value"
0
 
chrisrbloomCommented:
Does this help?  Referencing the Master Page as strongly typed...

http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx
0
 
jazzer102Author Commented:
Thanks I have tried this and the top of the user control page looks like this:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Content.ascx.vb" Inherits="Controls_Content" %>
<%@ MasterType virtualpath="~/site.master" %>

But I am getting an error The directive 'mastertype' is unkown.

I also tried <%@ Reference virtualpath="~/site.master" %> but get and error relating to a circular reference.

Any ideas?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
chrisrbloomCommented:
I misunderstood what you were trying to do.  Sorry about that.  I thought you were trying to reference the Master Page from content page controls.

If all the controls are embeded on the same page (not content pages) you should be able to reference them directly (ie no master...reference)


What is the name of your breadcrumb control?  Assuming it's "SiteMapPath1", then you should be able to reference it:

SiteMapPath1.ForeColor = Drawing.Color.Black





0
 
jazzer102Author Commented:
No I think you were right the first time around I am trying to reference a public property set up in the master page from a "User Control" (.ascx page) that is embedded in the master page

Also I am not using a sitemappath control - I am building it as a string and assigning it to a label on the page - It all works when the label is in the masterpage itself, but I need to assign it to a literal within a repeater on the user control page. My though was to assign the string to a public property set up in the masterpage, and then pick it up on the item databound handle of the repeater.

Hope this makes  sense!

What is confusing me is how difficult it seems to share a "variable" between user controls / master pages etc? Maybe Im missing something but when a page is executed on the server surely all the components ie the masterpage and user controls etc can "talk" to eachother somehow?? (I have even seen references on fourms to use session variables for this purpose which seems ridiculous!)
0
 
chrisrbloomCommented:
If the control you are building has a public property, then you should just be able to reference that control (and it's properties and methods) from the master page itself.
Create the custom control:
--call out the public property (ex URLText)

Public Property URLText() As String
        Get
            Return Label1.Text.ToString
        End Get
        Set(ByVal value As String)
            Label1.Text = value
        End Set
    End Property

In your master page, reference the custom control:

<%@ Register Src="../Controls/MyCustomControl.ascx" TagName="CustomControl" TagPrefix="uc1" %>

Then use a control in the code:
<uc1:CustomControl ID="CustomControl1" runat="server">....</uc1>

then in your codebehind, reference all the custom properties of your control (if using VS or VWD, the smart tags should show the properties)

CustomControl1.URLText = "Home Page"  
0
 
jazzer102Author Commented:
Right so I have essetially switched this around:
 In the masterpage I have
<%@ Register src="Controls/Content.ascx" tagname="ucContent" tagprefix="uc1" %>

and

<uc1:ucContent ID="Content1" runat="server" />

In the user control  code behind I have:

    Private _breadcrumbs As String
    Public Property breadcrumbs() As String
        Get
            Return _breadcrumbs
        End Get
        Set(ByVal value As String)
            _breadcrumbs = value
        End Set
    End Property

but when I try to access ucContent.breadcrumbs = _breadcrumbs in the master page code behind I get
'ucContent' is not declared. It may be inaccessible due to its protection level. Both in VS and when I try to run the site directly?
0
 
jazzer102Author Commented:
Nice one!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.