从子页面访问母版页用户控件

时间:2017-07-18 11:52:48

标签: c# asp.net

我有一个包含usercontrol的母版页。 现在Abc.aspx是该母版页的子页面。现在,子页面也有一个用户控件。 我的要求是获取母版页的用户控件表单子页面的用户控件。

母版页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>

用户控件aspx(在母版页中使用)

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

现在继承该母版页的子页面

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

现在子页面的用户控件

<%@ 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" />

现在子页面的用户控件代码隐藏

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

我的要求是隐藏MasterPage对此特定页面的用户控件。

更新 得到了解决方案。 感谢This Post

面临问题

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

2 个答案:

答案 0 :(得分:0)

首先,在母版页上使用FindControl找到用户控件(标识为UserControlOnMaster)。因此,位于页面上的用户控件中的某个位置使用此代码。

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

找到后,您可以访问该用户控件中的其他控件。

Label LabelOnMaster = control.FindControl("Label1") as Label;
LabelOnMaster.Text = "Control found!";

答案 1 :(得分:0)

由于UserControl在父级中为protected,因此您需要具有public函数来更新控件并从子级访问它。

示例,在母版页中:

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

现在只需在子页面中:

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);
        }
    }
}