1
using System.IO;
2
3
public partial class BookWithThemes : System.Web.UI.MasterPage
4

{
5
/**//// <summary>
6
/// Upon loading,populate theme list based on themes in App_Themes folder
7
/// </summary>
8
protected void Page_Load(object sender, EventArgs e)
9
{
10
// dynamically load the drop down list only the first time
11
if (!IsPostBack)
12
{
13
// themes must be in this location
14
string path = Server.MapPath("~/App_Themes");
15
// verify this location exists
16
if (Directory.Exists(path))
17
{
18
// retrieve array of theme folder names
19
String[] themeFolders = Directory.GetDirectories(path);
20
// process each element in this array
21
foreach (String folder in themeFolders)
22
{
23
// retrieve information about this folder name
24
DirectoryInfo info = new DirectoryInfo(folder);
25
// add this folder name to the drop-down list
26
drpThemes.Items.Add(info.Name);
27
}
28
// once all the themes are added to the list, we must make the
29
// content page's current theme the selected item in the list
30
31
// first search the list items for the page's theme name
32
ListItem item = drpThemes.Items.FindByText(Page.Theme);
33
// now set the selected index of the list (i.e., select that
34
// list item) to the index of the list item we just found.
35
drpThemes.SelectedIndex = drpThemes.Items.IndexOf(item);
36
}
37
}
38
}
39
40
/**//// <summary>
41
/// Event handler for the theme selector
42
/// </summary>
43
protected void drpTheme_selectedChanged(object s, EventArgs e)
44
{
45
//save theme in session
46
string theme = drpThemes.SelectedItem.Text;
47
Session["themeName"] = theme;
48
49
//re-request page
50
string page = Request.Path;
51
Server.Transfer(page);
52
}
53
54
/**//// <summary>
55
/// Property to get/set the url of the advertisement image
56
/// </summary>
57
public string AdImageUrl
58
{
59
get
{ return imgbtnAd.ImageUrl; }
60
set
{ imgbtnAd.ImageUrl = value; }
61
}
62
63
/**//// <summary>
64
/// Property to get/set the url for the link surrounding the
65
/// advertisement image.
66
/// </summary>
67
public string AdNavigateUrl
68
{
69
get
{ return imgbtnAd.NavigateUrl; }
70
set
{ imgbtnAd.NavigateUrl = value; }
71
}
72
}
73
using System.IO;2

3
public partial class BookWithThemes : System.Web.UI.MasterPage4


{5

/**//// <summary>6
/// Upon loading,populate theme list based on themes in App_Themes folder7
/// </summary>8
protected void Page_Load(object sender, EventArgs e)9

{10
// dynamically load the drop down list only the first time11
if (!IsPostBack)12

{13
// themes must be in this location14
string path = Server.MapPath("~/App_Themes");15
// verify this location exists16
if (Directory.Exists(path))17

{18
// retrieve array of theme folder names19
String[] themeFolders = Directory.GetDirectories(path);20
// process each element in this array21
foreach (String folder in themeFolders)22

{23
// retrieve information about this folder name24
DirectoryInfo info = new DirectoryInfo(folder);25
// add this folder name to the drop-down list26
drpThemes.Items.Add(info.Name);27
}28
// once all the themes are added to the list, we must make the 29
// content page's current theme the selected item in the list30

31
// first search the list items for the page's theme name32
ListItem item = drpThemes.Items.FindByText(Page.Theme);33
// now set the selected index of the list (i.e., select that 34
// list item) to the index of the list item we just found.35
drpThemes.SelectedIndex = drpThemes.Items.IndexOf(item);36
}37
}38
}39

40

/**//// <summary>41
/// Event handler for the theme selector 42
/// </summary>43
protected void drpTheme_selectedChanged(object s, EventArgs e)44

{45
//save theme in session 46
string theme = drpThemes.SelectedItem.Text;47
Session["themeName"] = theme;48

49
//re-request page50
string page = Request.Path;51
Server.Transfer(page);52
}53

54

/**//// <summary>55
/// Property to get/set the url of the advertisement image56
/// </summary>57
public string AdImageUrl58

{59

get
{ return imgbtnAd.ImageUrl; }60

set
{ imgbtnAd.ImageUrl = value; }61
}62

63

/**//// <summary>64
/// Property to get/set the url for the link surrounding the 65
/// advertisement image.66
/// </summary>67
public string AdNavigateUrl68

{69

get
{ return imgbtnAd.NavigateUrl; }70

set
{ imgbtnAd.NavigateUrl = value; }71
}72
}73

本文介绍了一个ASP.NET应用程序中实现主题切换功能的方法。通过读取App_Themes文件夹内的目录并将其作为选项填充到下拉列表中,允许用户在不同页面主题间进行选择。此外,还详细说明了如何在主题更改时保存所选主题,并重新加载当前页面。
9467

被折叠的 条评论
为什么被折叠?



