<span style="font: 12pt verdana; color:orange;font-weight:700" runat="server"> This is some literal text inside a styled span control</span> <p><font face="verdana"><h4>Styled Button</h4></font><p> <button style="font: 8pt verdana;background-color:lightgreen;border-color:black;width:100" runat="server">Click me!</button>
<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>
<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服务器控件的样式:
<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>
给Web服务器控件应用样式
Web 服务器控件添加了几个用于设置样式的强类型属性(例如背景色、前景色、字体名称和大小、宽度、高度等等),从而为样式提供了更多层次的支持。这些样式属性表现了HTML中可用的样式行为的子集,并表现为System.Web.UI.WebControls.WebControl基类直接暴露的"平面"属性。使用这些属性的优势在于,在开发工具(例如微软Visual Studio .NET)中,它们提供了编译时的类型检测和语句编译。