给Web服务器控件应用样式
Web 服务器控件添加了几个用于设置样式的强类型属性(例如背景色、前景色、字体名称和大小、宽度、高度等等),从而为样式提供了更多层次的支持。这些样式属性表现了HTML中可用的样式行为的子集,并表现为System.Web.UI.WebControls.WebControl基类直接暴露的"平面"属性。使用这些属性的优势在于,在开发工具(例如微软Visual Studio .NET)中,它们提供了编译时的类型检测和语句编译。
下面的例子显示了一个应用了几种样式的WebCalendar控件。请注意,当设置的属性是类类型(class type)的时候(例如字体),你必须使用子属性语法PropertyName-SubPropertyName(属性-子属性):
| <ASP:Calendar runat="server" BackColor="Beige" ForeColor="Brown" BorderWidth="3" …… /> |
| <ASP:Calendar runat="server" …… DayStyle-Width="50px" DayStyle-Height="50px" TodayDayStyle-BorderWidth="3" WeekEndDayStyle-BackColor="palegoldenrod" WeekEndDayStyle-Width="50px" WeekEndDayStyle-Height="50px" SelectedDayStyle-BorderColor="firebrick" SelectedDayStyle-BorderWidth="3" OtherMonthDayStyle-Width="50px" OtherMonthDayStyle-Height="50px" /> |
| <ASP:Calendar ... runat="server"> <TitleStyle BorderColor="darkolivegreen" BorderWidth="3" BackColor="olivedrab" Height="50px" /> </ASP:Calendar> |
| <ASP:Calendar id="MyCalendar" runat="server" …… > <TitleStyle BorderColor="darkolivegreen" BorderWidth="3" BackColor="olivedrab" Height="50px" /> <DayHeaderStyle BorderColor="darkolivegreen" BorderWidth="3" BackColor="olivedrab" ForeColor="black" Height="20px" /> <WeekEndDayStyle BackColor="palegoldenrod" Width="50px" Height="50px" /> <DayStyle Width="50px" Height="50px" /> <TodayDayStyle BorderWidth="3" /> <SelectedDayStyle BorderColor="firebrick" BorderWidth="3" /> <OtherMonthDayStyle Width="50px" Height="50px" /> </ASP:Calendar> |
| <style> .calstyle { font-size:12pt; font-family:Tahoma,Arial; } </style> <ASP:Calendar CssClass="calstyle" runat="server" …… /> |
| <ASP:TextBox runat="server" class="beige" style="font-weight:700;"/> <ASP:TextBox TextMode="Password" runat="server" class="beige"/> <ASP:DropDownList class="beige" runat="server"> <ASP:ListItem>Default Desktop</ASP:ListItem> <ASP:ListItem>My Stock Portfolio</ASP:ListItem> <ASP:ListItem>My Contact List</ASP:ListItem> </ASP:DropDownList> <ASP:Button Text="Submit" runat="server" class="beige"/> |
| <script language="VB" runat="server"> Sub Page_Load(Src As Object, E As EventArgs) Dim MyStyle As New Style MyStyle.BorderColor = Color.Black MyStyle.BorderStyle = BorderStyle.Dashed MyStyle.BorderWidth = New Unit(1) MyLogin.ApplyStyle (MyStyle) MyPassword.ApplyStyle (MyStyle) MySubmit.ApplyStyle (MyStyle) End Sub </script> Login: <ASP:TextBox id="MyLogin" runat="server" />/<p/> Password: <ASP:TextBox id="MyPassword" TextMode="Password" runat="server" /> View: <ASP:DropDownList id="MySelect" runat="server"> ... </ASP:DropDownList> |

