母版页

public   partial   class  母版页_Default : System.Web.UI.Page
{
    
protected void Page_PreInit(object sender, EventArgs e)  //在内容页和母版页合并之前运行的事件
    {
        
if (Session["aa"!= null)   //虚判断
        {
            Page.MasterPageFile 
= "~/母版页/mp2.master";  //获取或设置母版页的文件名
        }

    }

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
    }

    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        ((Label)Master.FindControl(
"Label1")).Text = DropDownList1.SelectedValue;  //获得母版页中Lable1控件对他赋值
        Label1.Text = DropDownList1.SelectedValue;
    }

}
重点:Page.MasterPageFile 
         Master.FindControl

2、检测不同的浏览器,应用不同的母版页
 1、属性定义

<% @ Page Language = " C# "  MasterPageFile = " ~/MasterPage/MasterPage1CS.master "  
 ie:MasterPageFile
= " ~/MasterPage/MasterPageForIE.master "  
 Safari:MasterPageFile
= " ~/MasterPage/MasterPageForSafari.master "  
 Mozilla:MasterPageFile
= " ~/MasterPage/MasterPageForMozilla.master "  
 Netscape:MasterPageFile
= " ~/MasterPage/MasterPageForNetscape.master "  
 Opera:MasterPageFile
= " ~/MasterPage/MasterPageForOpera.master "  
%>

 2、用代码动态检测
  protected   void  Page_PreInit( object  sender, EventArgs e)
    
{
        
if (Request.Browser.IsBrowser("IE"))
        
{
            
this.MasterPageFile = "MasterPageForIE.master";
        }

        
else if (Request.Browser.IsBrowser("Mozilla"))
        
{
            
this.MasterPageFile = "MasterPageForMpzilla.master";
        }

        
else if (Request.Browser.IsBrowser("Netscape"))
        
{
            
this.MasterPageFile = "MasterPageForNetscape";
        }

        
else
        
{
            
this.MasterPageFile = "MasterPage1CS.master";
        }

    }

你可能感兴趣的:(母版页)