Theme问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ThemeDemo.aspx.cs" Inherits="ThemeDemo" StylesheetTheme="RedTheme"%><!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>Theme Demos</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<asp:Label ID="Label3" runat="server" Text="选择页面主题"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Value="YellowTheme">请选择颜色</asp:ListItem>
<asp:ListItem Value="YellowTheme">Yellow</asp:ListItem>
<asp:ListItem Value="RedTheme">Red</asp:ListItem>
</asp:DropDownList><br />
........
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ThemeDemo : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = Request.QueryString["DropDownList1"];
}
}
为什么我在DropDownList1中选择值以后,页面的主题不变呢?(一直是默认设置的红色) --------------------编程问答-------------------- http://www.codeproject.com/useritems/dynamicThemes.asp 可以下載Demo,有源易做图,也有Article,應該就是你所需要的 --------------------编程问答-------------------- Page.Theme = Request.QueryString["DropDownList1"];
这一句有问题吧,代码能详细点吗?
--------------------编程问答-------------------- 在 Page_PreLoad内做这些事情 --------------------编程问答-------------------- to xiahouwen(武眉博<活靶子.NET>)
我看MSDN上说只能在 Page_PreInit内设置主题啊 --------------------编程问答-------------------- 一般在页面基类实现这个功能,这样只要每个页面都继承整个基类就可以变换theme。
public class BasePage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
……
……
base.OnPreInit(e);
}
}
--------------------编程问答-------------------- 嘿,认真点,“Request.QueryString["DropDownList1"];”这跟“DropDownList1”控件有关系吗?
认真。不会就多查联机帮助。 --------------------编程问答-------------------- 我照书上打的代码啊。
OK,我把代码贴全了
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ThemeDemo.aspx.cs" Inherits="ThemeDemo" StylesheetTheme="RedTheme" %>
<!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>Theme Demos</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label3" runat="server" Text="选择页面主题"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="YellowTheme" Selected="True">Yellow</asp:ListItem>
<asp:ListItem Value="RedTheme">Red</asp:ListItem>
</asp:DropDownList>
<br /><br /><br />
<table class="tableStyle">
<tr>
<td style="height:23px" colspan="2" class="tdStyle"></td>
</tr>
<tr>
<td style="width: 160px; text-align: right">
<asp:Label ID="Label1" runat="server" Text="您的名字"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 160px; text-align: right">
<asp:Label ID="Label2" runat="server" Text="您的昵称"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" /></td>
</tr>
<tr>
<td style="height:23px" colspan="2" class="tdStyle"></td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ThemeDemo : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
this.Theme = Request.QueryString["DropDownList1"];
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
问题就是选择后不更新主题。 --------------------编程问答-------------------- 上面的.cs文件内容错了,我重新贴一下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ThemeDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = Request.QueryString["DropDownList1"];
}
} --------------------编程问答-------------------- 嘿嘿,你能说说是什么书吗?简直是...... --------------------编程问答-------------------- 我想问是哪错了啊,帮忙解决一下好吗 --------------------编程问答-------------------- Request.QueryString["DropDownList1"]根本不能取得DropDownList1的值,这个写法去取得控件值,毫无道理,只能解释为睡着觉了,没有一点点合理的解释。
另外,即使处理了以上问题,从asp.net的设计上说,必须实现为postback,保存页面状态。不能用那种页面重定向的做法。
照你这里写法,可以写:
protected void Page_PreInit(object sender, EventArgs e)
{
if(Session["theme"]!=null)
Page.Theme = (string)Session["theme"];
}
void DropDownList1_SelectedIndexChanged(object sender,.....)
{
Session["theme"]=DropDownList1.SelectedValue;
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "reload",
this.Page.ClientScript.GetPostBackEventReference(this,null), true);
}
不一定使用Session。这里仅仅是偷懒举例而已。实际情况中,往往还可以使用数据库、cookie等。 --------------------编程问答-------------------- protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["Theme"] != null)
{
Page.Theme = Session["Theme"].ToString();
}
else
{
Session["Theme"] = "YellowTheme";
Page.Theme = Session["Theme"].ToString();
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Theme"] = RadioButtonList1.SelectedValue;
}
我晕了,我换了个控件RadioButtonList1,里面有两个item
text:Yellow value:YellowTheme
text:Red value:RedTheme
现在能换主题了,可出来怪毛病了,我点Yellow,他变成红色,我点Red他变成黄色。晕了,我theme里颜色没有写反,这是为什么吗 --------------------编程问答-------------------- 向前看 --------------------编程问答-------------------- 当你改变RadioButtonList1.SelectedValue时,必须表现它的ui。否则你改变了Session值当时并没有表现它,名不符实,逻辑肯定不对。可是theme规定必须写在PreInit(之前),必须刷新(reload)页面。第三个注意点是:这么简单的一个功能,必须实现为postback,如果实现为页面重定向就太笨拙了(页面上各种业务控件的状态全丢了)。 --------------------编程问答-------------------- 不太明白。。。。。
--------------------编程问答-------------------- page_init()方法
永远在
page_load()方法
执行之前,所以才出现以上问题! --------------------编程问答-------------------- <%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "ThemeDemo.aspx.cs " Inherits= "ThemeDemo " StylesheetTheme= "RedTheme " %> 中设置了StylesheetTheme= "RedTheme " ,去掉
补充:.NET技术 , ASP.NET