在分析ASP
NET页面的时候
在System
Web
UI
HtmlControls
HtmlControl类中
样式信息被填充到CssStyleCollection类型的Style属性
这个属性本质上是一个字典
它把控件的样式暴露为每个样式属性键的按字符串索引的值集合
例如
你可以使用下面的代码设置和检索HtmlInputText服务器控件的width样式属性
<script language=VB runat=server >
Sub Page_Load(Sender As Object E As EventArgs)
MyTextStyle(width) = px
ResponseWrite(MyTextStyle(width))
End Sub
</script>
<input type=text id=MyText runat=server/>
下面的例子显示了如何编程使用Style集合属性来控制HTML服务器控件的样式
<script language=VB runat=server>
Sub Page_Load(Src As Object E As EventArgs)
MessageInnerHtml &= <h>Accessing Styles</h>
MessageInnerHtml &= The color of the span is: & MySpanStyle(color) & <br>
MessageInnerHtml &= The width of the textbox is: & MyTextStyle(width) & <p>
MessageInnerHtml &= MySelects style collection is: <br><br>
Dim Keys As IEnumerator
Keys = MySelectStyleKeysGetEnumerator()
Do While (KeysMoveNext())
Dim Key As String
Key = CStr(KeysCurrent)
MessageInnerHtml &= <li>
MessageInnerHtml &= Key & = & MySelectStyle(Key) & <br>
Loop
End Sub
Sub Submit_Click(Src As Object E As EventArgs)
MessageInnerHtml &= <h>Modifying Styles</h>
MySpanStyle(color) = ColorSelectValue
MyTextStyle(width) =
MessageInnerHtml &= The color of the span is: & MySpanStyle(color) & <br>
MessageInnerHtml &= The width of the textbox is: & MyTextStyle(width)
End Sub
</script>
给Web服务器控件应用样式
Web 服务器控件添加了几个用于设置样式的强类型属性(例如背景色前景色字体名称和大小宽度高度等等)从而为样式提供了更多层次的支持这些样式属性表现了HTML中可用的样式行为的子集并表现为SystemWebUIWebControlsWebControl基类直接暴露的平面属性使用这些属性的优势在于在开发工具(例如微软Visual Studio NET)中它们提供了编译时的类型检测和语句编译
下面的例子显示了一个应用了几种样式的WebCalendar控件请注意当设置的属性是类类型(class type)的时候(例如字体)你必须使用子属性语法PropertyNameSubPropertyName(属性-子属性)
<ASP:Calendar runat=server
BackColor=Beige
ForeColor=Brown
BorderWidth=
……
/>
[] [] [] [] [] [] [] [] []