1

I have a master page which contains a usercontrol. Now Abc.aspx is the child page of that master page.Now, child page has also a user control. My requirement is to grab master page's user control form child page's user control.

Master Page aspx

<%@ Master Language="C#" MasterPageFile="~/masterhome.Master" AutoEventWireup="true" CodeBehind="lmsmasternew.master.cs" Inherits="e2aPortal.LMS.lmsmasternew" %>

<%@ Register Src="~/homeUserControl/UserProfilePic.ascx" TagPrefix="uc1" TagName="UserProfilePic" %>
<%@ Register Src="~/homeUserControl/MuduleListLeftPanel.ascx" TagPrefix="uc1" TagName="MuduleListLeftPanel" %>



<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title></title>
    <script src='<%# ResolveUrl("~/Scripts/jquery-1.4.1-vsdoc.js") %>' type="text/javascript"></script>
    <script src='<%# ResolveUrl("~/Scripts/jquery-1.11.1.min.js") %>'></script>
    <link href='<%# ResolveUrl("~/StyleSheet/profilesidebar.css") %>' rel="stylesheet" />
    <link href='<%# ResolveUrl("~/StyleSheet/font-awesome.css") %>' rel="stylesheet" />
    <link href='<%# ResolveUrl("~/Content/bootstrap.min.css") %>' rel="stylesheet" />
    <script type="text/javascript">
        function openpage(pagename) {

            $("#maincontent").load(pagename + ".aspx #maincontent", function () {
                // make content visible with effect   

            });
        }
    </script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="form-group">
        <div class="row" style="margin-top: 5%;">
            <div class="col-xs-3">
                <uc1:UserProfilePic runat="server" ID="UserProfilePic" />
                <asp:Label runat="server" ID="lbl1"></asp:Label>
                <br />
                <uc1:MuduleListLeftPanel runat="server" ID="MuduleListLeftPanel" />
            </div>
            <div class="col-xs-9">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </div>
        </div>
    </div>
</asp:Content>

User control aspx (Used in Master page)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MuduleListLeftPanel.ascx.cs" Inherits="e2aPortal.homeUserControl.MuduleListLeftPanel" %>

Now child page that inherits that master page

<%@ Page Title="" Language="C#" MasterPageFile="~/LMS/lmsmasternew.master" AutoEventWireup="true" CodeBehind="CreateQuestionTemplate.aspx.cs" Inherits="e2aPortal.LMS.CreateQuestionTemplate" %>

<%@ Register Src="~/LMS/UserControl/CreateTemplate.ascx" TagPrefix="uc1" TagName="CreateTemplate" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <uc1:CreateTemplate runat="server" ID="CreateTemplate" />
</asp:Content>

Now child page's user control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CreateTemplate.ascx.cs" Inherits="e2aPortal.LMS.UserControl.CreateTemplate" %>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css" rel="stylesheet" type="text/css" />

Now child page's user control codebehind

protected void Page_Load(object sender, EventArgs e)
        {
            MuduleListLeftPanel control = Page.Master.FindControl("MuduleListLeftPanel") as MuduleListLeftPanel;
            //Label control1 = Page.Master.FindControl("lbl1") as Label;

            if (control != null)
            {
                control.Visible = false;  // will not going to execute :D
            }
        }

My requirement is to hide MasterPage's usercontrol for this specific page.

Update: Got my solution. Thanks for This Post

Problem facing

MuduleListLeftPanel muduleListLeftPanel = this.Master.LeftPanel;
UserProfilePic userProfile = this.Master.UserProfile;
muduleListLeftPanel.Visible = false; // hide sucessfully
userProfile.Attributes["style"] = "display:none"; // non working .. I need to use display none.. for both user control
Babai
  • 99
  • 1
  • 14
  • Possible duplicate of [how to access master page control from content page](https://stackoverflow.com/questions/15573505/how-to-access-master-page-control-from-content-page) – Marco Jul 18 '17 at 12:02
  • No, I want to access MasterPage's user control..please read my question again. – Babai Jul 18 '17 at 13:26
  • Finally got a solution & thanks to [this](https://stackoverflow.com/questions/15760361/get-and-cast-masterpage-usercontrol-from-content-page-to-access-specific-uc-prop) post – Babai Jul 19 '17 at 13:03

2 Answers2

0

First, use FindControl on the Master Page to locate the User Control (with ID UserControlOnMaster). So somewhere in the User Control located on a Page, use this code.

WebUserControl1 control = Page.Master.FindControl("UserControlOnMaster") as WebUserControl1;

When found you can then access the other Controls in that User Control.

Label LabelOnMaster = control.FindControl("Label1") as Label;
LabelOnMaster.Text = "Control found!";
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Yes, I have done that way still no help from my code..protected void Page_Load(object sender, EventArgs e) { System.Web.UI.UserControl control = Page.Master?.FindControl("MuduleListLeftPanel") as System.Web.UI.UserControl; // fetching user control from child page if (control == null) { throw new NullReferenceException("Invalid object"); } } – Babai Jul 18 '17 at 15:35
  • You have to search for the correct usercontrol type, not `UserControl` but `WebUserControl1` as in my answer – VDWWD Jul 18 '17 at 15:41
  • sir, I have updated my code but still got the same error. I have also posted the code that I am using. – Babai Jul 18 '17 at 16:47
  • Your `lbl1` is not inside the User Control, but on the Master itself (that would be `Page.Master.FindControl("lbl1")`). If you want to hide the Control, why not use `control.Visible = false;` – VDWWD Jul 18 '17 at 19:38
  • Thanks for your reply sir, Actually, I was testing by putting a label on my master page, I thought there might be some problem while accessing UserControl , but I was wrong, the same issue is happening for Label also. I have updated my code @VDWWD sir – Babai Jul 18 '17 at 19:45
  • made this working but stuck in another. Thanks for your help. :) – Babai Jul 19 '17 at 15:28
0

Since the UserControl is protected in the parent you need to have a public function to update the control and access it from the child.

Example, in the master page:

 public partial class SiteMaster : System.Web.UI.MasterPage
 {
     public void SetMyUserControlVisibility(bool visible)
     {
         MyUserControl.Visible= visible;
     }
 }

Now simply in the child page:

public partial class MyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SiteMaster masterPage = (SiteMaster)this.Page.Master;

            // update master page's user control here
            masterPage.SetMyUserControlVisibility(true);
        }
    }
}
Ali Kleit
  • 3,069
  • 2
  • 23
  • 39