home *** CD-ROM | disk | FTP | other *** search
-
- <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
-
- <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
-
- <h4>Applying Styles to Controls</h4>
- <p>
-
- <div class="indent" style="font-family:Verdana; font-size:8pt;">
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b><a class="toc2" target="content" href="#htmlstyles">Applying Styles to HTMLControls</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b><a class="toc2" target="content" href="#webstyles">Applying Styles to Web Controls</a><br>
- </div>
- <p>
- <hr>
-
- The web is an extremely flexible environment for user interfaces, with extreme variations in look-and-feel across web sites. The
- widespread adoption of Cascading Style Sheets (CSS) is largely responsible for the rich designs we encounter on the web. ASP+
- has been designed to provide first-class support for CSS styles in all of its HTML Controls and Web Controls. In this section we demonstrate
- the use of styles in conjunction with server controls, and how it is possible to have very fine control over the look-and-feel of your
- Web Forms.
-
- <!--BEGIN SECTION-->
- <br>
- <a name="htmlstyles">
- <br>
- <span class="subhead">Applying Styles to HTML Controls</span>
- <p>
-
- Standard HTML tags support CSS through a "style" attribute, which can be set to a semicolon-delimited list of attribute/value pairs.
- A good resource for learning about the various CSS attributes supported by the Internet Explorer browser can be found on
- MSDN Web Workshop's <a target="_blank" href="http://msdn.microsoft.com/workshop/author/css/reference/attributes.asp">CSS Attributes Reference</a> page.
- All of the ASP+ HTML Controls can accept styles in exactly the same manner as standard HTML tags. The following example
- illustrates a number of styles applied to various HTML Controls. If you "view source" on the page returned to the client, you'll note
- that these styles are passed along to the browser in the control's rendering.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style1.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style1.src"
- Icon="/quickstart/aspplus/images/style1.gif"
- Caption="Style1.aspx"
- runat="server" />
-
-
- <p>
- CSS also defines a "class" attribute, which can be set to a CSS style definition contained in a <style>...</style> section of
- the document. The class attribute makes it easy to define styles once and apply them to several tags without the need to redefine the
- style itself. Styles on HTML Controls may also be set in this manner, as demonstrated by the sample below.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style2.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style2.src"
- Icon="/quickstart/aspplus/images/style2.gif"
- Caption="Style2.aspx"
- runat="server" />
-
- <p>
- When an ASP+ page is parsed, the style information is populated into a <b>Style</b> property (of type <b>CssStyleCollection</b>)
- on the <b>System.Web.UI.HtmlControls.HtmlControl</b> class. This property is essentially a dictionary which exposes the control's
- styles as String-indexed collection of values for each style-attribute key. For example, you could do the following to set and
- subsequently retrieve the width style-attribute on an HtmlInputText control:
-
- <div class="code"><pre>
- <script runat="server>
-
- public void Page_Load(Object sender, EventArgs E)
- {
- MyText.Style["width"] = "90px";
- Response.Write(MyText.Style["width"]);
- }
-
- </script>
-
- <input type="text" id="MyText" runat="server"/>
- </pre></div>
-
- <p>
- This next sample shows how you can programmatically manipulate the style for an HTML Control using this Style collection
- property.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style3.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style3.src"
- Icon="/quickstart/aspplus/images/style3.gif"
- Caption="Style3.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <br>
- <a name="webstyles">
- <br>
- <span class="subhead">Applying Styles to Web Controls</span>
- <p>
-
- ASP+ Web Controls provide an additional level of support for styles by adding several strongly-typed properties for commonly used
- style settings, such as background and forground color, font name and size, width, height, etc. These style properties represent a
- sub-set of all style behavior available in HTML, and are represented as "flat" properties exposed directly on the
- <b>System.Web.UI.WebControls.WebControl</b> base class. The advantage to using these properties is that they provide
- compile-time type checking and statement completion in development tools, such as Microsoft Visual Studio 7.
- <p>
- This next sample shows a WebCalendar control with several styles applied to it (a calendar without styles applied is included for
- contrast). Note that when setting a property which is a class-type like "Font", we use the sub-property syntax
- <i>PropertyName-SubPropertyName</i> .
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style4.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style4.src"
- Icon="/quickstart/aspplus/images/style4.gif"
- Caption="Style4.aspx"
- runat="server" />
-
- <p>
- The System.Web.UI.WebControls namespace includes a <b>Style</b> base class which encapsulates common style
- attributes (additional style classes, such as <b>TableStyle</b> and <b>TableItemStyle</b> inherit from
- this common base class). Many Web Controls expose properties of this type for specifying the style of individual rendering
- elements of the control. For example, the WebCalendar exposes many such Style properties: <b>DayStyle</b>,
- <b>WeekendDayStyle</b>, <b>TodayDayStyle</b>, <b>SelectedDayStyle</b>, <b>OtherMonthDayStyle</b>,
- and <b>NextPrevStyle</b>. We can set individual properties of these Styles using the sub-property syntax:
- <i>PropertyName-SubPropertyName</i>. The following sample demonstrates this:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style5.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style5.src"
- Icon="/quickstart/aspplus/images/style5.gif"
- Caption="Style5.aspx"
- runat="server" />
-
- <p>
- The sub-property syntax can get a little unwieldy when setting many Style properties at once. A slightly different syntax allows
- each Style property to be declared as a child control to the Web Control:
-
- <p>
- <div class="code"><pre>
- <ASP:Calendar ... runat="server">
- <property name="TitleStyle">
- <ASP:TableItemStyle BorderColor="darkolivegreen" BorderWidth="3" BackColor="olivedrab" Height="50px" />
- </property>
- </ASP:Calendar>
- </pre></div>
- <p>
-
- The following sample is functionally equivalent to the preceding sample, using this alternative syntax:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style6.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style6.src"
- Icon="/quickstart/aspplus/images/style6.gif"
- Caption="Style6.aspx"
- runat="server" />
-
- <p>
- Just as you can apply styles to HTML Controls using a CSS class definition, you can also do this with Web Controls. The
- WebControl base class exposes a String property named <b>CssClass</b> for setting the style class:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style7.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style7.src"
- Icon="/quickstart/aspplus/images/style7.gif"
- Caption="Style7.aspx"
- runat="server" />
-
- <p>
- If an attribute is set on a server control which does not correspond to any strongly-typed property on the control, the attribute
- and value are populated in the <b>Attributes</b> collection of the control. By default, server controls will render these attributes
- unmodified in the HTML returned to the requesting browser client. This means that the "style" and "class" attributes may be
- set on Web Controls directly instead of using the strongly-typed properties. While this requires some understanding of the actual
- rendering of the control, it can be a flexible way to apply styles as well. It is especially useful for the standard form input controls,
- as illustrated in the sample below:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style8.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style8.src"
- Icon="/quickstart/aspplus/images/style8.gif"
- Caption="Style8.aspx"
- runat="server" />
-
- <p>
- Web Control Styles may also be set programmatically, using the <b>ApplyStyle</b> method of the base WebControl class.
- For example:
- <p>
-
- <div class="code"><pre>
- <script runat="server">
- protected void Page_Load(Object Src, EventArgs E )
- {
- Style style = new Style();
- style.BorderColor = Color.Black;
- style.BorderStyle = BorderStyle.Dashed;
- style.BorderWidth = 1;
-
- MyLogin.ApplyStyle (style);
- MyPassword.ApplyStyle (style);
- MySubmit.ApplyStyle (style);
- }
- </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>
- </pre></div>
-
- <p>
- The following example demonstrates the above code:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/customize/style9.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/customize/style9.src"
- Icon="/quickstart/aspplus/images/style9.gif"
- Caption="Style9.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <a name="endofsection">
-
- <h4>Section Summary</h4>
- <ol>
- <li>ASP+ provides first-class support for CSS styles in both its HTML Controls and Web Controls families.
- <li>Styles may be applied by setting either the "style" or "class" attributes of a controls. These settings are accessible
- programmatically through the control's Attributes collection. In the case of HTML Controls, individual values for style-attribute
- keys may be retrieved from the control's Style collection.
- <li>Most commonly used style settings are exposed on Web Controls as strongly-typed properties of the control itself.
- <li>The System.Web.UI.WebControls namespace includes a Style base class which encapsulates common style
- attributes. Many Web Controls expose properties of this type to to control individual rendering elements.
- <li>Styles may be set programmatically on Web Controls using the ApplyStyle method of the WebControl base class.
- </ol>
- <p>
-
- <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->