Solved

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

Posted on 2011-02-17
8
742 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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