您的位置:首页 > 编程语言 > ASP

[ASP.NET学习笔记之十二]ASP.NET 2.0中Theme、MasterPage和代码国际化

2006-08-22 08:54 786 查看
ASP.NET2.0之Theme(主题)
使用主题

定义的主题样式后缀为skin
指定主题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Theme="myTheme"%>
在这个页面编辑模式时的控件样式未发生变化,浏览的时候就会发生变化,这个是典型的例子。对于我这样不懂美化界面的人才说,定义样式还是比较难的,因为在编辑模式当中无法直接预览样式。可以把Theme换成StylesheetTheme属性, 这样就可以直接在编辑模式中显示结果样式了。这里存在着样式修改的优先级.定义StylesheetTheme属性的话,可以在页面上再次修改样式.Theme优先级别最高。结果都是一样的
使用themes
1、在1个页面中应用Theme:
如果想在某1个页面中应用Theme,直接在aspx文件中修改<%@ Page Theme="..." %>,比如你想这个页面应用default2 theme,设置<%@ Page Theme="..." %>就OK

2、在所有页面应用同1个Theme:
如果要在所有页面上使用相同的Theme,在web.config中的<system.web>节点下加上句<pages theme="..."/>
3、让控件不应用Theme:
有时我们不希望应用.skin中的风格,这时你只需设置控件的EnableTheming属性为false的时候就可以了。

另外要注意的是定义了Theme,页面的属性设置会失效。要页面属性生效。需要使用StylesheetTheme代替Theme。如下代码所示
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" StylesheetTheme="myTheme"%>
除此之外,我们还可以通过代码访问Theme。注意Page.Theme一定要在Page_PreInit事件之中或者之前定义,代码如下:
protected void Page_PreInit()
{
Page.Theme = Server.HtmlEncode("myTheme");
}

还可以指定空间使用特性的样式
比如两个IMG控件,指定两个不同的图片,需指定SkinID属性
<asp:ImageButton ID="ImageButton1" runat="server" SkinID="OKButton" />
<asp:ImageButton ID="ImageButton2" runat="server" SkinID="CancelButton" />

不使用主题,使用CSS来改变

使用Style类
void Page_Load(Object sender, EventArgs e)
{
Style myStyle = new Style();

myStyle.BackColor = Color.Yellow;
myStyle.ForeColor = Color.Green;
myStyle.BorderStyle = BorderStyle.Dashed;
myStyle.BorderWidth = new Unit(4);

txtTextBox1.ApplyStyle( myStyle );
txtTextBox2.ApplyStyle( myStyle );
txtTextBox3.MergeStyle( myStyle );
}
添加样式表
<%@ Page Language="C#" %>

<script runat="Server">

void lbtnScript_Click(object s, EventArgs e)
{
myLabel.CssClass = "myClass1";
}

void lbtnVerdana_Click(object s, EventArgs e)
{
myLabel.CssClass = "myClass2";
}

</script>

<html>
<head>
<style>

.myClass1
{
font: 18pt script;
color: blue;
}

.myClass2
{
font: 24pt verdana;
color: red;
}

</style>
<title>CssClassDynamic.aspx</title>
</head>
<body>
<form id="Form1" runat="Server">
<asp:Label ID="myLabel" Text="Here is some text!" runat="Server" />
<hr>
<asp:LinkButton ID="lbtnScript" Text="Show Script!" OnClick="lbtnScript_Click" runat="Server" />
<asp:LinkButton ID="lbtnVerdana" Text="Show Verdana!" OnClick="lbtnVerdana_Click" runat="Server" />
</form>
</body>
</html>

控件应用style属性的顺序如下:
a、StyleSheetTheme引用的风格
b、代码设定的控件属性(覆盖StyleSheetTheme)
c、Theme引用的风格(覆盖前面2个)

ASP.NET2.0之MasterPage
在网上看到一段关于MasterPage和aspx页面的区别
You create a Master Page in pretty much the same way as you create a normal ASP.NET page. A Master Page can contain the same Web controls, User controls, HTML content, and scripts that you would add to a standard ASP.NET page. There are three important differences between a Master Page and a normal ASP.NET page.
First, unlike a normal ASP.NET page, the name of a Master Page must end with the special extension .master. This extension marks the page as a Master Page. Furthermore, an ASP.NET application is configured so that you cannot request pages with the .master extension. It doesn't make sense to request a Master Page directly. Instead, you request content pages based on the Master Page.
Second, a Master Page includes a <%@ Master %> directive instead of the normal <%@ Page %> directive. The <%@ Master %> directive supports many of the same attributes as the <%@ Page %> directive. For example, you can specify the programming language of the page with the directive <%@ Master Language="vb" %>.
The final difference between a Master Page and a normal ASP.NET page is that a Master Page can contain zero or more ContentPlaceHolder controls. A ContentPlaceHolder control can be used only within a Master Page. This control marks an area of a Master Page that can be overridden by a particular content page.[引用地址]

类似Theme的应用方式,我们做一个简单的例子,对于需要在不同场合由于不同内容来替换的地方用:ContentPlaceHolder组件来定义就行。这样,一个模板就制作好了。
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="myMasterPage.master.cs" Inherits="myMasterPage" %>

<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
下面建立一个你要显示内容的ASPX页面。将里面的所有html元素全清掉。注意保留下:
<%@ Page Language="C#" MasterPageFile="~/myMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

或者是在Page_PreInit事件添加Master页
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "~/myMasterPage.master";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: