?
Solved

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

Posted on 2011-02-17
8
Medium Priority
?
754 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:jazzer102
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:chrisrbloom
ID: 34915974
Does this help?  Referencing the Master Page as strongly typed...

http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx
0
 

Author Comment

by:jazzer102
ID: 34916601
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
 
LVL 3

Expert Comment

by:chrisrbloom
ID: 34916807
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jazzer102
ID: 34917129
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
 
LVL 3

Expert Comment

by:chrisrbloom
ID: 34917393
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
 

Author Comment

by:jazzer102
ID: 34918452
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
 
LVL 3

Accepted Solution

by:
chrisrbloom earned 2000 total points
ID: 34919270
try this:

Content1.breadcrumbs = "some text value"
0
 

Author Closing Comment

by:jazzer102
ID: 34924503
Nice one!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question