This tip will give complete knowledge of how to change master page, render controls and accessing its eventhandlers @ runtime.

Introduction

This tip is for those who are looking for basic/advanced knowledge about how to change Master page @runtime and how to render controls @ runtime. This will allow user to save his/her personal settings just like in gmail.

Using the Code

Just download the attached zip file and extract it. This application is developed using ASP.NET 4.0 framework.

Solution explorer view is as below for your reference.

Create a Base class for all Master pages named "BaseMaster". This will handle Page_Load event of all Master Pages.

using System.Web.UI.WebControls;

public class BaseMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.RawUrl == "/Settings.aspx")
{
DropDownList drop = new DropDownList();
drop.ID = "DropDownThemes";
drop.Items.Add(new ListItem("Site1", "Site1.Master"));
drop.Items.Add(new ListItem("Site2", "Site2.Master"));
drop.Items.Add(new ListItem("Site3", "Site3.Master"));
drop.Enabled = true;
drop.AutoPostBack = true;
drop.SelectedIndexChanged += new EventHandler(this.drop_SelectedIndexChanged);
if (Session["MasterPage"] != null)
{
drop.SelectedValue = Session["MasterPage"].ToString();
}
try
{
((Site1)this).dropDownTheme = drop;
}
catch
{
try
{
((Site2)this).dropDownTheme = drop;
}
catch
{
((Site3)this).dropDownTheme = drop;
}
}
}
} protected void drop_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Session["MasterPage"] = ((Site1)this).dropDownTheme.SelectedValue.ToString();
}
catch
{
try
{
Session["MasterPage"] = ((Site2)this).dropDownTheme.SelectedValue.ToString();
}
catch
{
Session["MasterPage"] = ((Site3)this).dropDownTheme.SelectedValue.ToString();
}
} Response.Redirect(Request.Url.AbsoluteUri);
}
}

Here, I am creating a DropDownList control and assigning to MasterPage. Eventhandler "SelectedIndexChanged" is assigned to the same.

Inherit this BaseMaster class to all Master pages, say Site1.Master, Site2.Master, Site3.Master.

public partial class Site1 : BaseMaster
{ public DropDownList dropDownTheme
{
get { return (DropDownList)this.PlaceHolder1.FindControl("DropDownThemes"); }
set { PlaceHolder1.Controls.Add((DropDownList)value); }
}
}

Notice that I added a Public property "dropDownTheme". This property is created because here I am accessing the dropdown control from "BaseMaster" class not in same class "Site1".

HTML of Site1 is here. Same HTML is used in remaining Master page also (Site2.Master and Site3.Master).

<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site1.master.cs" Inherits="WebApplication4.MasterPages.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">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body style="background-color: Aqua">
<form id="form1" runat="server">
This is Site 1 Master page
<table>
<tr>
<td style="text-align: left">
<h1>
This is Header</h1>
</td>
<td style="text-align: right; color: Red">
<a href="../../Settings.aspx"
id="LinkToSettings">Settings</a>
</td>
</tr>
<tr>
<td>
This is Menu section from Master<br />
<asp:PlaceHolder ID="PlaceHolder1"
runat="server"></asp:PlaceHolder>
</td>
<td>
<fieldset>
<legend>Content Page</legend>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</fieldset>
</td>
</tr>
<tr style="text-align: center">
<td colspan="2">
<small>All Rights Reserved with no one.</small>
</td>
</tr>
</table>
</form>
</body>
</html>

Create a content page "Settings.aspx" HTML is below:

<%@ Page Title="" Language="C#"
MasterPageFile="~/MasterPages/Site1.Master" AutoEventWireup="true"
CodeBehind="Settings.aspx.cs" Inherits="WebApplication4.Settings" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
This is Settings Page with Master<br>
<asp:Button ID="btnBack" runat="server" Text="Back"
onclick="btnBack_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset"
onclick="btnReset_Click" />
</asp:Content>

In the code behind page, add the below events:

public partial class Settings : BasePage
{
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("HomePage.aspx");
} protected void btnReset_Click(object sender, EventArgs e)
{
Session["MasterPage"] = "Site1.Master" ;
Response.Redirect(Request.Url.AbsoluteUri);
}
}

Here, this drop down will render only on Settings.aspx page. If there are any other content pages, dopdown will not be shown as only settings page is designed to allow so.

Create a Class file and write the below code to choose Master page.

public class BasePage : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["MasterPage"] != null)
{
MasterPageFile = WebConfigurationManager.AppSettings
["MasterFileFolder"] + Session["MasterPage"].ToString();
}
}
}

All the webpages which will use Master page have to inherit this class. This will handle Page_preInt event for all pages.

Add a master page folder path to .config file:

<appSettings>
<add key="MasterFileFolder" value="~\MasterPages\"/>
</appSettings>

Enjoy the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

How to Change Master Page @ Run-time的更多相关文章

  1. 自定义SharePoint2013 master page

    SharePoint uses templates to define and render the pages that a site displays. The structure of a Sh ...

  2. Creating a New Master Page in SharePoint 2013

    Creating a New Master Page in SharePoint 2013 This article explains how to create a Master Page in S ...

  3. Adding a WebPart to a SharePoint 2013 Master Page 分类: Sharepoint 2015-07-08 01:03 7人阅读 评论(0) 收藏

    On SharePoint 2013 you can not add the Web Parts to the master page the same way of 2010. Please use ...

  4. 通过SharePoint Designer对SharePoint 2010的Master Page进行自定制

    1:需要在对应的SiteCollection 和 Site 中开启Publishing的服务 2:在Designer中创建自己的Master Page,进行对原始v4.master代码进行复制,和修改 ...

  5. sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page

    转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...

  6. 转载 SharePoint 2013配置Master Page and Page Layout

    转载原地址: http://www.cnblogs.com/huangjianwu/p/4539706.html 涉及到的内容是关于SharePoint 2013如何部署自定义的母版页和布局页. 进入 ...

  7. 【Sharepoint】CSS与Master Page的开发与部署

    一.CSS的开发与部署相对比较简单,先是要上传CSS文件到样式库中,然后在页面模板中将上传的自定义CSS样式文件设置为所有文件的默认css文件.下面详细的介绍CSS文件的开发与部署过程. 1.编写自定 ...

  8. mysql change master导致gtid丢失

    change master导致gtid丢失从innobackupex恢复导致binlog的拉取位置会导致主备gtid不一致.此类错误通过构造空事务方式无法修复.此时就需要change master 方 ...

  9. 安装Docker时错误提示 "could not change group /var/run/docker.sock to docker: group docker not found"的解决方案

    安装Dock服务,主要命令是  yum install docker. 但是在启动的时候报错:warning msg="could not change group /var/run/doc ...

随机推荐

  1. ios上传图片遇见了一个TimeoutError(DOM Exception 23)异常

    TimeoutError(DOM Exception 23):The operation timed out 百度了下,没发现解决办法

  2. Castle IOC概念理解

    最近在用Castle,里面有些概念比较容易混淆,特此解释一下: 1. 容器(Container):Windsor是一个反转控制容器.它创建在一个微内核的基础之上,这个微内核能够扫描类并且试图找到这些类 ...

  3. jsp中的session

    浏览器和服务器的异常通话 常用方法 setAttribute(String key,Object value);//设置值 getAttribute(String key); //取值 Invalid ...

  4. HTML中的颜色简写

    1.HTML中颜色的五种写法 1)直接用颜色英文名字表示 例如表示背景颜色为白色: 2.通过16进制数表示 例如表示背景颜色为黑色 3).通过RGB方式表示 RGB:是红色(red)绿色(green) ...

  5. Java一般要学多久?

    其实学java一般要多久?因人而异,有些人资质好,头脑聪明几个月就能学会,有些人天生愚钝,理解能力差,不过勤能补拙,只是时间相对长点 要坚持住.不过java相对于C,C++java而言,java无疑简 ...

  6. java 线程基础学习

    今天趁空闲时间看了点线程方面的知识 首先看的是volatile关键字,按照我之前书上看到的一点知识,自己的理解是,volatile关键字会阻止编译优化,因为cpu每次读取数据是并不是从高速缓存中读取, ...

  7. HTML`CSS_网站页面不同浏览器兼容性问题解决

    目前,最为流行的浏览器共有五个:分别是ie,Edge浏览器(属于微软),火狐,谷歌(chrome)Safari和Opera五大浏览器. Trident内核:IE ,360,,猎豹,百度: Gecko内 ...

  8. OracleAWR删除历史快照说明

    测试时,发现无法产生新快照,查看系统时间为10月26,但是已经产生快照为12月1号了. 此时的解决办法,就是删除现有的快照. 转http://itlab.idcquan.com/Oracle/back ...

  9. 布局类,让多个div在一行显示

    原文链接:http://www.divcss5.com/wenji/w472.shtml

  10. 微软发布WCF教程及大量示例

    继前面 微软公司发布Windows Communication Foundation (WCF)和Windows CardSpace的示例程序之后,微软今天又发布了WF的教程和大量示例,对于学习WF的 ...