โดยปกติแล้วผมจะใช้ Skin กำหนด Style ให้กับ Web Control ไม่ว่าจะเป็น GridView,SiteMapPath,Menu แล้วใน Skin ก็จะกำหนดว่าค่า Style Class จะเรียกใช้ CSS Class ตัวไหนอีกที ขอยกตัวอย่างคราวๆ
NagavigateSkin.skin
<asp:SiteMapPath SkinID="SiteMapPathSkin" runat="server"
CurrentNodeStyle-CssClass="SiteMapPathCurrentNodeStyle"
NodeStyle-CssClass="SiteMapPathNodeStyle"
PathSeparatorStyle-CssClass="SiteMapPathSeparatorStyle"
RootNodeStyle-CssClass="SiteMapPathRootNodeStyle">
</asp:SiteMapPath>
<asp:Menu SkinID="MenuSkin" runat="server"
CssClass="MenuStyle"
DynamicMenuItemStyle-CssClass="DynamicMenuItemStyle"
DynamicHoverStyle-CssClass="DynamicMenuHoverStyle"
DynamicMenuStyle-CssClass="DynamicMenuStyle"
DynamicSelectedStyle-CssClass="DynamicMenuSelectedStyle"
StaticSelectedStyle-CssClass="MenuSelectedStyle"
StaticMenuStyle-CssClass="MenuStyle"
StaticMenuItemStyle-CssClass="MenuItemStyle"
StaticHoverStyle-CssClass="MenuHoverStyle">
</asp:Menu>
NagavigateStyle.css
/* ============================================
Menu Style
=============================================== */
.MenuStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
height: 25px;
text-decoration:none;
/*background-color: #FEFEFE;
border: 1px solid #000000;*/
}
.MenuItemStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
padding: 2px 2px 2px 2px!important;
}
.MenuHoverStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
background-color: #9DCE5C;
border: 1px solid #000000;
text-decoration:none!important;
/*background-color: #FFFFCC!important; */
}
.MenuSelectedStyle
{
font-family:Verdana!important;
font-size:8pt!important;
/*background-color:Lime!important;*/
}
/*================================
Dynamic menu Style Section
================================== */
.DynamicMenuStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
padding: 2px 5px 2px 5px!important;
border: 1px solid #000000;
margin-top: -1px;
background-color: #9DCE5C!important;
z-index: 10000;
text-decoration:none;
}
.DynamicMenuItemStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
padding: 2px 2px 2px 2px!important;
border: 1px solid #9DCE5C;
z-index: 10;
text-decoration:none;
/*background-color: #FFCC99!important;*/
}
.DynamicMenuHoverStyle
{
font-family:Verdana!important;
font-size:8pt!important;
color: #000000!important;
background-color: #C7EB6E;
border: 1px solid #000000;
z-index: 10000;
text-decoration:none!important;
/*background-color: #FFFFCC!important; */
}
.DynamicMenuSelectedStyle
{
font-family:Verdana!important;
font-size:8pt!important;
background-image: url(Images/BulletMenu.gif);
z-index: 10000;
/*background-color:Lime!important;*/
}
/* ====================================
SiteMap Style
======================================= */
a.SiteMapPathRootNodeStyle:link,
a.SiteMapPathRootNodeStyle:visited,
a.SiteMapPathRootNodeStyle:active
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: #FFFFFF;
font-weight:bold;
text-decoration:none;
}
a.SiteMapPathRootNodeStyle:hover
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: orange;
font-weight:bold;
text-decoration:none;
}
.SiteMapPathCurrentNodeStyle
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: #FFFFFF;
font-weight:bold;
}
.SiteMapPathNodeStyle
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: #FFFFFF;
font-weight:bold;
}
a.SiteMapPathNodeStyle,
a.SiteMapPathNodeStyle:link,
a.SiteMapPathNodeStyle:visited,
a.SiteMapPathNodeStyle:active
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: #FFFFFF;
font-weight:bold;
text-decoration:none;
}
a.SiteMapPathNodeStyle:hover
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
color: Orange;
font-weight:bold;
text-decoration:none;
}
.SiteMapPathSeparatorStyle
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size:8pt;
font-weight:bold;
color: #FFCC33;
}
Apply Skin to Menu,SiteMapPath Web Control
<asp:SiteMapPath ID="SiteMapPath1" runat="server" SkinID="SiteMapPathSkin" SkipLinkText="" PathSeparator=" :: "></asp:SiteMapPath>
*** ชื่อ SkinId จะตรงกับ Skin Id ที่ระบุใน NagavigateSkin.skin
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" SkinID="MenuSkin" SkipLinkText="" DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2" MaximumDynamicDisplayLevels="5">
<DataBindings>
<asp:MenuItemBinding DataMember="siteMapNode" NavigateUrlField="url" TextField="title" />
</DataBindings>
<DynamicMenuStyle BorderColor="Black" BorderWidth="1px" HorizontalPadding="5px" VerticalPadding="2px" />
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
ปล. ไฟล์ Css ที่ผมให้เป็นตัวอย่างมีการใช้รูปด้วย แต่ผมไม่ได้ upload ให้นะรับ ดังนั้นการแสดงผลจะไม่เหมือนที่ผมทำ
Web Project Structure
[Web Root Project]
+ App_Theme
- - - + Forest <== ชื่อ Theme
- - - - - - NagavigateSkin.skin
- - - - - - NagavigateStyle.css
+ MasterPages
- - - ContentMasterPage.master <== master page file
- Default.aspx
- web.config
จาก project structure จะเห็นได้ว่าไฟล์ skin,css ที่ใช้ อยู่ใน theme Forest ดังนั้นผมจึงกำหนด default theme = Forest ใน web.config โดยแก้ไขเพิ่มประมาณนี้ครับ
Web.config
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages theme="Forest" />
</system.web>
</configuration>