这是根据上篇转载的文章写的事例 :
PageBase.cs
Web.config
blue.css
red.css
Master1.master
Master2.master
Default.aspx
Default.aspx.cs
PageBase.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
/// <summary>
12
/// PageBase 的摘要说明
13
/// </summary>
14
public class PageBase:Page
15
{
16
public PageBase()
17
{
18
}
19
20
protected override void OnPreInit(EventArgs e)
21
{
22
base.OnPreInit(e);
23
System.Web.Profile.ProfileBase MyProfile = HttpContext.Current.Profile;
24
this.MasterPageFile = MyProfile.GetPropertyValue("MasterFilePreference").ToString();
25
this.Theme = MyProfile.GetPropertyValue("ThemePreference").ToString();
26
}
27
}
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10

11
/// <summary>12
/// PageBase 的摘要说明13
/// </summary>14
public class PageBase:Page15
{16
public PageBase()17
{18
}19
20
protected override void OnPreInit(EventArgs e)21
{22
base.OnPreInit(e);23
System.Web.Profile.ProfileBase MyProfile = HttpContext.Current.Profile;24
this.MasterPageFile = MyProfile.GetPropertyValue("MasterFilePreference").ToString();25
this.Theme = MyProfile.GetPropertyValue("ThemePreference").ToString();26
}27
}Web.config
1
<?xml version="1.0" encoding="utf-8"?>
2
<configuration>
3
<appSettings/>
4
<connectionStrings/>
5
<system.web>
6
<profile>
7
<properties>
8
<add name="ThemePreference" type="string" defaultValue="blue"/>
9
<add name="MasterFilePreference" type="string" defaultValue="~/Master1.master"/>
10
</properties>
11
</profile>
12
<compilation debug="false" />
13
<authentication mode="Windows" />
14
</system.web>
15
</configuration>
<?xml version="1.0" encoding="utf-8"?>2
<configuration>3
<appSettings/>4
<connectionStrings/>5
<system.web>6
<profile>7
<properties>8
<add name="ThemePreference" type="string" defaultValue="blue"/>9
<add name="MasterFilePreference" type="string" defaultValue="~/Master1.master"/>10
</properties>11
</profile>12
<compilation debug="false" />13
<authentication mode="Windows" />14
</system.web>15
</configuration>blue.css
1
body
2
{
3
background-color:blue;
4
}
5
body 2
{3
background-color:blue;4
}5

red.css
1
body
2
{
3
background-color:Red;
4
}
5
body 2
{3
background-color:Red;4
}5

Master1.master
1
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master1.master.cs" Inherits="Master1" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head runat="server">
5
<title>Master1.master</title>
6
</head>
7
<body>
8
<form id="form1" runat="server">
9
<div>
10
<div>
11
masterpage1</div>
12
<table cellpadding="0" border="0" cellspacing="0" width="100%">
13
<tr>
14
<td align ="left">
15
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
16
</asp:ContentPlaceHolder>
17
</td>
18
</tr>
19
</table>
20
</div>
21
</form>
22
</body>
23
</html>
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master1.master.cs" Inherits="Master1" %>2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">3
<html xmlns="http://www.w3.org/1999/xhtml">4
<head runat="server">5
<title>Master1.master</title>6
</head>7
<body>8
<form id="form1" runat="server">9
<div>10
<div>11
masterpage1</div>12
<table cellpadding="0" border="0" cellspacing="0" width="100%">13
<tr>14
<td align ="left">15
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">16
</asp:ContentPlaceHolder>17
</td>18
</tr>19
</table>20
</div>21
</form>22
</body>23
</html>Master2.master
1
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master2.master.cs" Inherits="Master2" %>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head runat="server">
5
<title>Master2.master</title>
6
</head>
7
<body>
8
<form id="form1" runat="server">
9
<div>
10
<div>
11
masterpage2</div>
12
<table cellpadding="0" border="0" cellspacing="0" width="100%">
13
<tr>
14
<td align="right">
15
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
16
</asp:ContentPlaceHolder>
17
</td>
18
</tr>
19
</table>
20
</div>
21
</form>
22
</body>
23
</html>
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master2.master.cs" Inherits="Master2" %>2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">3
<html xmlns="http://www.w3.org/1999/xhtml">4
<head runat="server">5
<title>Master2.master</title>6
</head>7
<body>8
<form id="form1" runat="server">9
<div>10
<div>11
masterpage2</div>12
<table cellpadding="0" border="0" cellspacing="0" width="100%">13
<tr>14
<td align="right">15
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">16
</asp:ContentPlaceHolder>17
</td>18
</tr>19
</table>20
</div>21
</form>22
</body>23
</html>Default.aspx
1
<%@ Page Language="C#" MasterPageFile="~/Master1.master" AutoEventWireup="true" CodeFile="Default1.aspx.cs"
2
Inherits="Default" Title="Untitled Page" %>
3
4
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
5
<div>
6
<h2>
7
Pick your master file:
8
<asp:DropDownList ID="ddlMasterFilePreference" runat="server">
9
<asp:ListItem Text="Site Choice One" Value="~/Master1.Master" />
10
<asp:ListItem Text="Site Choice Two" Value="~/Master2.Master" />
11
</asp:DropDownList>
12
</h2>
13
<h2>
14
Pick your theme preference:
15
<asp:DropDownList ID="ddlThemePreference" runat="server">
16
<asp:ListItem Text="blue" />
17
<asp:ListItem Text="red" />
18
</asp:DropDownList>
19
</h2>
20
<div>
21
<asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateBtn_Click" />
22
</div>
23
</div>
24
</asp:Content>
<%@ Page Language="C#" MasterPageFile="~/Master1.master" AutoEventWireup="true" CodeFile="Default1.aspx.cs"2
Inherits="Default" Title="Untitled Page" %>3

4
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">5
<div>6
<h2>7
Pick your master file:8
<asp:DropDownList ID="ddlMasterFilePreference" runat="server">9
<asp:ListItem Text="Site Choice One" Value="~/Master1.Master" />10
<asp:ListItem Text="Site Choice Two" Value="~/Master2.Master" />11
</asp:DropDownList>12
</h2>13
<h2>14
Pick your theme preference:15
<asp:DropDownList ID="ddlThemePreference" runat="server">16
<asp:ListItem Text="blue" />17
<asp:ListItem Text="red" />18
</asp:DropDownList>19
</h2>20
<div>21
<asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateBtn_Click" />22
</div>23
</div>24
</asp:Content>Default.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class Default : PageBase
13
{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
if (!IsPostBack)
17
{
18
ddlMasterFilePreference.SelectedValue = Profile.MasterFilePreference;
19
ddlThemePreference.SelectedValue = Profile.ThemePreference;
20
}
21
}
22
protected void UpdateBtn_Click(object sender, EventArgs e)
23
{
24
Profile.MasterFilePreference = ddlMasterFilePreference.SelectedValue;
25
Profile.ThemePreference = ddlThemePreference.SelectedValue;
26
Response.Redirect(Request.RawUrl);
27
28
}
29
}
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
public partial class Default : PageBase13
{14
protected void Page_Load(object sender, EventArgs e)15
{16
if (!IsPostBack)17
{18
ddlMasterFilePreference.SelectedValue = Profile.MasterFilePreference;19
ddlThemePreference.SelectedValue = Profile.ThemePreference;20
}21
}22
protected void UpdateBtn_Click(object sender, EventArgs e)23
{24
Profile.MasterFilePreference = ddlMasterFilePreference.SelectedValue;25
Profile.ThemePreference = ddlThemePreference.SelectedValue;26
Response.Redirect(Request.RawUrl);27

28
}29
}


浙公网安备 33010602011771号