Adding a CSS Style & Class To An ASP.NET Page Header

Posted by | Filed under , ,

 There are a variety of techniques out there for wiring up a CSS class to an ASP.NET Web Form through code. You can register a link to a CSS file via Page.Header.Controls.Add() and ASP.NET 2.0 introduced the robust WebResource framework for registering entire style sheets for a given page, but what about when you want to add a dynamic style to the HTML  header?

Typically, the technique would involve something like this:

Page.Header.Controls.Add(
    new LiteralControl(
        @"<style type='text/css'>
                /*type selector*/
                BODY
                {
                    background: Aqua;
                }
                /*class selector*/
                .myClass
                {
                    background: WhiteSmoke;
                    font-size: 20pt;
                }
                </style>
            "

        )
);

...but there are drawbacks to the approach including the need for verbose "style" tags with each chunk of CSS styles you want to add and the lack of validation on style names and values.

Luckily, there's a more sophisticated and elegant approach supported by .NET Framework 2.0 or higher using Page.Header.Stylesheet and the System.Web.UI.WebControls.Style class. With the approach, instead of having to define your CSS class using a string, you can used strongly-typed objects instead! Additionally, all of the styles you add will get added to one single style attribute in the Page header.

Style typeStyle = new Style();
typeStyle.BackColor = Color.Aqua;
Page.Header.StyleSheet.CreateStyleRule(typeStyle, null, "BODY");

Style classStyle = new Style();
classStyle.BackColor = Color.WhiteSmoke;
classStyle.Font.Size = FontUnit.Parse("20pt");
Page.Header.StyleSheet.CreateStyleRule(classStyle, null, ".MyClass");

There is, however, a drawback to the approach which is the limited number of properties available in the Style base class. For instance, if you wanted to set a value for the "margin" CSS style, you'd need to either use or create a subclass of Style that overrides the AddAttributesToRender() method.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

A Cool Way To Handle ASP.NET Properties Using Nullable Types

Posted by | Filed under , ,

In ASP.NET 1.x, it used to be common to handle the getter of a value type properties thusly:

public int SomeInteger
{
         get
         {
                object val = ViewState["SomeInteger"];
                return val != null ? (int)val : 0;
          }
}

Not bad, but there's more code than needed with nullable types and the "null coalescing operator", anyway. Here's the newer, sleeker way to do the same thing with half the space!

public int SomeInteger
{
         get{ return ViewState["SomeInteger"] as int? ?? 0; }
}

So what's going on in this snippet? Well, we cast the value to a nullable int (int?) which holds a reference to either an int or null then we use the aforementioned ?? operator to return our default value or 0 in the case of null

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5