Solved

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

Posted on 2011-02-17
8
738 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
React or Angular? 6 52
Class object 2 26
Remove greater than sign 3 42
Ajax Panel Not Updating When Triggered By Timer 4 37
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

914 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

18 Experts available now in Live!

Get 1:1 Help Now