ASP.NET为页面某个控件单独设置外观

有时我们给一个ASP.NET页面指定使用了一个主题后,又想单独为某个控件设置外观(不使用默认外观),这时可采用的方法有两种:一是使用命名的控件外观,二是在@Page指令中使用StylesheetTheme属性来代替常用的Theme。下边使用具体实例说明下两种方法的使用:

1.使用命名的控件外观

App_Themes\Default\Default.skin:
<%-- 默认的控件外观:未定义SkinId的TextBox控件都使用此外观。 --%>
<asp:TextBox runat="server" BorderWidth="1px" ForeColor="Black"></asp:TextBox>

<%-- 命名的控件外观:定义SkinId的TextBox控件将使用对应的外观。 --%>
<asp:TextBox runat="server" BorderWidth="1px" BorderColor="Blue" ForeColor="Blue" SkinId="Blue"></asp:TextBox>
<asp:TextBox runat="server" BorderWidth="1px" BorderColor="Red" ForeColor="Red" SkinId="Red"></asp:TextBox>

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" Theme="Default"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>主题和皮肤应用示例-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" SkinId="Blue"></asp:TextBox><br />
        <asp:TextBox ID="TextBox3" runat="server" SkinId="Red"></asp:TextBox>
    </div>
    </form>
</body>
</html>

2.@Page指令中使用StylesheetTheme属性来指定使用的主题

App_Themes\Default\Default.skin:
<%-- 默认的控件外观:未定义SkinId的TextBox控件都使用此外观。 --%>
<asp:TextBox runat="server" BorderWidth="1px" BorderColor="Black" ForeColor="Black"></asp:TextBox>

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>主题和皮肤应用示例-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" BorderWidth="1px" BorderColor="Black" ForeColor="Black"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" BorderWidth="1px" BorderColor="Blue" ForeColor="Blue"></asp:TextBox><br />
        <asp:TextBox ID="TextBox3" runat="server" BorderWidth="1px" BorderColor="Red" ForeColor="Red"></asp:TextBox>
    </div>
    </form>
</body>
</html>

说明:如果控件的同一种样式在外观文件和控件属性中都有定义的话, 使用Theme时则定义在外观文件中的样式发生作用,使用StylesheetTheme时则是定义在控件属性中的样式起作用!

上一篇: Javascript运算符学习笔记
下一篇: ASP.NET动态加载主题示例
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 3933
发表评论
登录后再发表评论!