Wintellect  

Monday, October 08, 2007 - Posts

Thanks everyone for the comments and tips offered after I gave this presentation at Alabama Code Camp, as well as, at the Atlanta Cutting Edge .NET group last week.  I received several "so that's how it works" comments and that was exactly the point of this talk.

Here is the abstract of the presentation:

This will be a thorough discussion of all that is CSS.  Whether you know it as the necessary evil or the great enabler (that just hasn’t quite clicked for you yet), you should walk away with something valuable from this discussion.  I will begin with the basic box model and travel all the way to the holiest of grails (the no tables here, two and three column ASP.NET Master Page layout…yours to take home for free!).  Along the way, we’ll touch on some CSS Best Practices and gotchas in ASP.NET and take a look at the new CSS tools in Visual Studio 2008 (Orcas).

Get the download here.

Note:  The solution file provided is from Visual Studio 2008 (Orcas) Beta 2.  There is nothing .NET 3.5 specific (as most of it is HTML anyway).   

Update 04/01/08:  Updated the download to compile under Visual Studio 2008 RTM

Finally able to grab a minute to post my code and slides from the Introduction to Generics presentation that I did this past weekend at the Alabama Code Camp at the University of Alabama. 

This is the abstract from the presentation:

With the release of the 2.0 version of the .NET Framework, Generics became first class citizens

in the Common Language Runtime.  Yet, many still shy away from using them because of perceived difficulty or other misconceptions.   This presentation will seek to dispel a few of these myths and offer a gentle introduction into using Generics on a daily basis.  Along the way, I’ll also demonstrate language enhancements in the .NET 3.5 runtime that lend themselves nicely to working with Generics. 

 

Get the download here.  

Note:  The code is compiled on Visual Studio 2008 (Orcas) Beta 2.  The only project in the solution that uses .NET 3.5 specific code is the Collections project.  This uses C# 3.5 Automatic Properties and Property Initializers. 

I just returned from speaking at the Alabama Code Camp 5.0 in Birmingham.  One of my presentations was Deep Dive CSS for the ASP.NET Developer and I had hoped to spend some time on the tools in Orcas for working with Cascading Style Sheets.  Unfortunately, I did not get a chance to do that due to the time constraints, so I wanted to post some of my better links on that subject...

How to: Use the Apply Styles and Manage Styles Windows
http://msdn2.microsoft.com/en-us/library/bb398979(VS.90).aspx

            How to: Use the CSS Properties Window
           
http://msdn2.microsoft.com/en-us/library/bb398902(VS.90).aspx

How to: Use the Direct Style Application Toolbar
http://msdn2.microsoft.com/en-us/library/bb398977(VS.90).aspx

 

Although this was originally part of my recent Generics presentation, I have received several requests to publish it separately. 

 

The reason that I created this originally was that I found myself often-times wanting a strongly-typed list of all the checkboxes / buttons / etc in my code-behind/beside pages.  There is the Page.FindControl(string id), but that only allows you to get a control by id and it returns a Control.  I wanted something more specific, yet generic enough to use as a Utility.  This was screaming for Generic Methods, so below is what I came up with.

 

I use this constantly and hope that someone else gets some mileage out of it.  If you have improvements, please post them.  For example usage, download the full Generics presentation. 

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace r2musings.Web.UI.WebControls

{

  public static class Utility

  {

    #region FindControlsByType

   

    /// <summary>

    ///   Returns a generic list of controls of a provided type starting at a

    ///    a provided base control (works recursively)

    ///    Example Usage: 

    ///       List<Button> buttonList = Utility.FindControlsByType<Button>(testPanel);

    ///       This would return a list of all Buttons contained anywhere within testPanel

    /// </summary>

    /// <typeparam name="T">Type of control</typeparam>

    /// <param name="parentControl">Base control to start search</param>

    /// <returns></returns>

    public static List<T> FindControlsByType<T>(Control parentControl) where T: System.Web.UI.Control

    {

      // new up our return list

      List<T> returnList = new List<T>();

 

      // loop through all controls and call internal recursion to

      //   add all controls of type T to the returnList

      foreach (Control childControl in parentControl.Controls)

      {

        InternalFindControlsByType<T>(childControl, returnList);

      }

     

      // return our List<T>

      return returnList;

    }

   

    #endregion

 

    #region Recursion Method

   

    /// <summary>

    ///   Should NOT call this method directly

    ///   This is for the internal recursion of FindControlsByType() 

    /// </summary>

    /// <typeparam name="T">Type of control</typeparam>

    /// <param name="parentControl">Base control to start search</param>

    /// <param name="returnList">List to add Controls</param>

    private static void InternalFindControlsByType<T>(Control parentControl, List<T> returnList) where T: System.Web.UI.Control

    {

      if (returnList == null)

        throw new ArgumentNullException("Null List passed to InternalFindControlsByType");

 

      if (parentControl is T)

      {

        returnList.Add((T) parentControl);

      }

 

      foreach (Control childControl in parentControl.Controls)

      {

        // call this method recursively to get all child controls

        InternalFindControlsByType(childControl, returnList);

      }

    }

   

    #endregion

  }

}