ASP.NET实现动态主题切换实例

项目获取地址:
https://download.csdn.net/download/qq_45968259/12882544

项目内容

在页面中,通过选择相应的主题,实现对页面应用所选主题。默认情况,页面应用主题一样式。执行程序,示例运行结果如图1、2所示。
在这里插入图片描述

在这里插入图片描述

项目结构

在这里插入图片描述

设计页面

在这里插入图片描述

代码

1、主题一
SkinFile.skin代码:

<asp:TextBox  runat="server" BackColor="#FF66FF" BorderColor="Aqua" Font-Size="12pt" ForeColor="#FFFF66" Width="150px">主题1</asp:TextBox>
<asp:TextBox Skinid="textboxSkin" runat="server" BackColor="#996600" BorderColor="Yellow" BorderStyle="Dotted" Font-Size="15pt" Width="250px">主题1</asp:TextBox>

StyleSheet.css代码:

body
{
text-align: center;
	color:darkgreen;
	background-color: #00FF00;
	background-image: url( 'images/bizhi3.jpg' );
	background-size:100% 100%; 
	background-attachment:fixed;
}
a:link
{
	color: #CCCCFF;
	text-decoration: underline;
}
a:visited
{
	color: #FF0000;
	text-decoration: underline;
}
a:hover
{
	color: #CC33FF;
	text-decoration: underline;
	font-style: italic;
}
input
{
	border-color: #0000FF;
}

2、主题二
SkinFile.skin代码:

<asp:TextBox  runat="server" BackColor="#666633" BorderColor="#CC00FF" BorderStyle="Groove" BorderWidth="3px" ForeColor="Red">主题2</asp:TextBox>
<asp:TextBox Skinid="textboxSkin" runat="server" BackColor="#FF6666" BorderColor="Aqua" Height="100px"  Width="200px">主题2</asp:TextBox>

StyleSheet.css代码:

body
{
	text-align: center;
	color:darkblue;
	background-color: #CC66FF;
	background-image: url(  'images/bizhi2.jpg' );
	background-size:100% 100%; 
	background-attachment:fixed;
}
a:link
{
	color: #0066FF;
	text-decoration: underline;
}
a:visited
{
	color: #009900;
	text-decoration: underline;
}
a:hover
{
	color: #000099;
	text-decoration: underline;
	font-style: italic;
}
input
{
	border-color: #FF0000;
}

后台代码
在PreInit事件发生过程中修改Page对象的Theme属性值。其代码如下:

protected void Page_PreInit(object sender, EventArgs e)
    {
        string theme = "Theme1";
        if (Request.QueryString["theme"] == null)
        {
            theme = "Theme1";
        }
        else
        {
            theme = Request.QueryString["theme"];
        }
        Page.Theme = theme;
        ListItem item = DropDownList1.Items.FindByValue(theme);
        if (item != null)
        {
            item.Selected = true;
        }
        TextBox2.SkinID = "textboxSkin";
    }

当用户选择任一个选项时,都会触发DropDownList控件的SelectedIndexChanged事件,在该事件下,将选项的主题名存放在myurl的QueryString(即theme)中,并重新加载页面。其代码如下:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string myurl = Request.Path + "?theme=" + DropDownList1.SelectedItem.Value;
        Response.Redirect(myurl);
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LongTermism

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值