locked
communicate between user control on masterpage and page (and then user control on page) RRS feed

  • Question

  • User-579688761 posted

    Hi I have a user control on my masterpage and I am trying to cast the parent page as a strong type. I cant find the right syntax to resolve the page class reference. I have read quite a few posts that seem to skate around the issue I am having or my be I'm just not seeing the point.

    In the user control I have some code which is called from the (Default.aspx) page_init

    Public Property dbmlVBCtrl As MyDataContext
     Get
      If mdbmlVBctrl Is Nothing Then
    '' go off to see if we can load this from the parent page Dim pt As Type = Page.GetType() If pt.Name = "default_aspx" Then Dim st As String = pt.UnderlyingSystemType.ToString()
    ''st values {Name = "default_aspx" FullName = "ASP.default_aspx"} Dim parentPage As default_aspx = TryCast(Page, default_aspx)
    '' build error here default_aspx is not defined Dim parentPage As ASP.default_aspx = TryCast(Page, ASP.default_aspx)
    '' build error here ASP.default_aspx is not defined
    End If mdbmlVBctrl = New VBCode.MyDataContext(Tools.GetConnectionString()) mynDbmlVBctrlLoaded = True End If Return mdbmlVBctrl End Get Set(ByVal Value As MyDataContext) mdbmlVBctrl = Value End Set End Property

    Friday, February 15, 2019 2:43 PM

All replies

  • User475983607 posted

    Just follow the same approach you would use to expose master page members (server controls) to the content page.  

    https://stackoverflow.com/questions/15573505/how-to-access-master-page-control-from-content-page

    Friday, February 15, 2019 4:00 PM
  • User-943250815 posted

    When you have a Master Page to get any control, you hahe to use Mater.FindControl. So if I want to get text of a label in Master Page

    Dim myLabel As Label = CType(Master.FindControl("myLabelID"), Label)
    Dim lblStr as string = myLabel.Text 



    Friday, February 15, 2019 4:07 PM
  • User-579688761 posted

    Thanks for the answer but the link says : you can then easily access...

    Problem is it does not appear easy to me. The access of the property relies on the Master page being hard typed and this seems to break down where trying to access the property of a user control is concerned. Unless you can give me an actual example please?

    Friday, February 15, 2019 8:11 PM
  • User-579688761 posted

    Thanks, but I want to access a  public (vb) Property  on the master page not a control this is where I'm having the problem

    Friday, February 15, 2019 8:28 PM
  • User475983607 posted

    ex glider pilot

    Thanks for the answer but the link says : you can then easily access...

    Problem is it does not appear easy to me. The access of the property relies on the Master page being hard typed and this seems to break down where trying to access the property of a user control is concerned. Unless you can give me an actual example please?

    Remember a user control is just a type (class).  When placed inside another class (master page) is becomes a member of that class.  In general we expose members through properties, methods, and events.  In this case we'll use properties.

    User Control where the label is hard coded for demo purposes.

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SimpleUc.ascx.cs" Inherits="WebFormsIdentity.UserControls.SimpleUc" %>
    <asp:Label ID="Message" runat="server" Text="Hello from the user control"></asp:Label>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsIdentity.UserControls
    {
        public partial class SimpleUc : System.Web.UI.UserControl
        {
            public string Text
            {
                get
                {
                    return this.Message.Text;
                }
                set
                {
                    this.Message.Text = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    The master page also has a property which exposes the SimpleUc Text property.

            <div class="container body-content">
                <div>
                    <uc1:SimpleUc runat="server" id="SimpleUc" />
                </div>           
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
                </footer>
            </div>
        public partial class SiteMaster : MasterPage
        {
    
            public string SimpleUcText
            {
                get
                {
                    return this.SimpleUc.Text;
                }
                set
                {
                    this.SimpleUc.Text = value;
                }
            }

    The content page grabs the SimpleUc text value and assigns the value to the Result label. Note the page directive provides intellisense access to the master page.

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SimplePage.aspx.cs" Inherits="WebFormsIdentity.SimplePage" %>
    <%@ MasterType VirtualPath="~/Site.Master" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <asp:Label ID="Result" runat="server" Text=""></asp:Label>
    </asp:Content>
    
    namespace WebFormsIdentity
    {
        public partial class SimplePage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Result.Text = Master.SimpleUcText;
            }
        }
    }

    The results are

    Hello from the user control
    Hello from the user control

    Now you can set the SimpleUc text from the content page like so.

    namespace WebFormsIdentity
    {
        public partial class SimplePage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Result.Text = Master.SimpleUcText;
                Master.SimpleUcText = "Hello from the content page";
            }
        }
    }

    Results

    Hello from the content page
    Hello from the user control

    Friday, February 15, 2019 9:24 PM