在分析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) 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> | 上一页 [1] [2] |