Modifying Control Appearance

One of the great things about ASP.NET Server Controls is that they are incredibly flexible in allowing developers to define how they should appear on the page. Most server controls expose properties that allow simple formatting, such as fonts and background colors. All server controls also expose properties that allow for setting cascading style sheets (CSS) styles to modify the appearance of a control. Finally, some controls allow the use of templates to further define how the output of the control should appear. Together or individually, these techniques allow ASP.NET developers extensive control over the appearance of their controls.

Properties

Using control properties is the simplest and least powerful technique for modifying the appearance of a control. Example 5-12 shows a page with two Label controls, one of which uses its default settings. The second Label control has one attribute used to set the Font-Name property. Font is a property that is represented by the FontInfo class. Setting the Font-Name attribute sets the value of the FontInfo class’ Name member. Note that this second Label control also has the BackColor property set (in this case, to blue) in the Page_Load event handler. The output from Example 5-12 is shown in Figure 5-8.

Example 5-12. ControlProps.aspx

<%@ Page Language="vb" %> <html> <head> <title>Control Properties Example</title> <script runat="server"> Sub Page_Load( ) Label2.BackColor = System.Drawing.Color.LightBlue End ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.