asp.net2.0的主題和皮膚特性使你能夠把樣式和布局信息存放到一組獨(dú)立的文件中,總稱為主題(theme)。接下來我們可以把這個主題應(yīng)用到任何站點(diǎn),用于改變該站點(diǎn)內(nèi)的頁面和控件的外觀和感覺。通過改變主題的內(nèi)容,而不用改變站點(diǎn)的單個頁面,就可以輕易地改變站點(diǎn)的樣式。主題也可以在開發(fā)者之間共享。
asp.net包含了大量的用于定制應(yīng)用程序的頁面和控件的外觀和感覺的特性。控件支持使用style(樣式)對象模型來設(shè)置格式屬性(例如字體、邊框、背景和前景顏色、寬度、高度等等)。控件也支持使用樣式表(css)來單獨(dú)設(shè)置控件的樣式。你可以用控件屬性或css來定義控件的樣式信息,或者把這些定義信息存放到單獨(dú)的一組文件中(稱為主題),然后把它應(yīng)用到程序的所有或部分頁面上。單獨(dú)的控件樣式是用主題的皮膚(skin)屬性來指定的。
本文用大量的示例演示了在asp.net 2.0中如何使用樣式、主題和皮膚特性。
給控件應(yīng)用樣式
web用戶界面是非常靈活的,不同的web站點(diǎn)的外觀和感覺是截然不同的。目前廣泛采用的樣式表(css)在很大程度上就是負(fù)責(zé)處理web上遇到的豐富的設(shè)計(jì)需求的。asp.net的html服務(wù)器控件和web服務(wù)器控件都被設(shè)計(jì)成優(yōu)先支持css樣式表。這一部分討論如何在服務(wù)器控件上使用樣式,并演示了它們所提供的web窗體的外觀和感覺的非常細(xì)微的控制。
給html控件應(yīng)用樣式
標(biāo)準(zhǔn)的html標(biāo)記通過style屬性來支持css,我們可以用分號隔離的屬性/值對(pair)來設(shè)置它。所有的asp.net html服務(wù)器控件都可以采用標(biāo)準(zhǔn)html標(biāo)記的方式來接受樣式。下面的例子演示了大量的應(yīng)用到html服務(wù)器控件的樣式。在源代碼中你可以看到,這些樣式都是在控件顯示的時候傳遞給瀏覽器的。
<span runat="server"> 
this is some literal text inside a styled span control</span> 
<p><font face="verdana"><h4>styled button</h4></font><p> 
<button runat="server">click me!</button> 
css還定義了class屬性,你可以把它設(shè)置為文檔中<style>...</style>內(nèi)包含的css樣式定義。class屬性使你能夠一次定義樣式,在多個服務(wù)器標(biāo)記上使用,避免了樣式的重復(fù)定義。html服務(wù)器控件的style屬性可以用這種方式來設(shè)置,如下所示:
<style> 
.spanstyle  
{  
font: 12pt verdana;  
font-weight:700; 
color:orange; 
} 
.buttonstyle  
{  
font: 8pt verdana; 
background-color:lightgreen; 
border-color:black; 
width:100  
} 
…… 
</style> 
<span class="spanstyle" runat="server"> 
this is some literal text inside a styled span control 
</span> 
<p><font face="verdana"><h4>styled button</h4></font><p> 
<button class="buttonstyle" runat="server">click me!</button> 
在分析asp.net頁面的時候,在system.web.ui.htmlcontrols.htmlcontrol類中,樣式信息被填充到cssstylecollection類型的style屬性。這個屬性本質(zhì)上是一個字典,它把控件的樣式暴露為每個樣式屬性鍵的按字符串索引的值集合。例如,你可以使用下面的代碼設(shè)置和檢索htmlinputtext服務(wù)器控件的width樣式屬性:
<script language="vb" runat="server" > 
sub page_load(sender as object, e as eventargs) 
mytext.style("width") = "90px" 
response.write(mytext.style("width")) 
end sub 
</script> 
<input type="text" id="mytext" runat="server"/> 
下面的例子顯示了如何編程使用style集合屬性來控制html服務(wù)器控件的樣式:
<script language="vb" runat="server"> 
sub page_load(src as object, e as eventargs) 
message.innerhtml &= "<h5>accessing styles...</h5>" 
message.innerhtml &= "the color of the span is: " &myspan.style("color") &"<br>" 
message.innerhtml &= "the width of the textbox is: " &mytext.style("width") &"<p>" 
message.innerhtml &= "myselect's style collection is: <br><br>" 
dim keys as ienumerator 
keys = myselect.style.keys.getenumerator() 
do while (keys.movenext()) 
dim key as string 
key = cstr(keys.current) 
message.innerhtml &= "<li>  " 
message.innerhtml &= key &"=" &myselect.style(key) &"<br>" 
loop 
end sub 
sub submit_click(src as object, e as eventargs) 
message.innerhtml &= "<h5>modifying styles...</h5>" 
myspan.style("color") = colorselect.value 
mytext.style("width") = "600" 
message.innerhtml &= "the color of the span is: " &myspan.style("color") &"<br>" 
message.innerhtml &= "the width of the textbox is: " &mytext.style("width") 
end sub 
</script> 
新聞熱點(diǎn)
疑難解答
圖片精選