Solved

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

Posted on 2011-02-17
8
735 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 500 total points
ID: 34919270
try this:

Content1.breadcrumbs = "some text value"
0
 

Author Closing Comment

by:jazzer102
ID: 34924503
Nice one!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now