从内容页设置母版页中控件的属性

Site1.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Lib.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div class="page">
            <asp:Menu ID="VisitorMenu" runat="server" />
        </div>
    </form>
</body>
</html>

Site1.Master.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lib
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Menu theVisitorMenu
        {
            get { return VisitorMenu; }
            set { VisitorMenu = value; }
        }

    }
}

 homepage.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="homepage.aspx.cs" Inherits="Lib.homepage" %>
<%@ MasterType VirtualPath="~/Site1.master" %> <!--关键,也有设置TypeName的,h ttp://msdn.microsoft.com/zh-cn/library/ms228274(VS.80).aspx-->

<asp:Content ID="HeaderContent" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>

homepage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lib
{
public partial class homepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Master.theVisitorMenu.Visible = true;
}
}

}
}




/**************************************************************************
                  原文来自博客园——Submarinex的博客: www.cnblogs.com/submarinex/               
  *************************************************************************/

原文地址:https://www.cnblogs.com/submarinex/p/2296570.html