DNN Admin Control Styling

This is a simple, clean style for use in the admin sections of DotNetNuke controls.

#promotionalSpace { width:100%; font-family:Arial; text-align:left; margin-bottom:15px; }
    #promotionalSpace .shadow { width:100%; text-align:center; padding:20px 0; }
    #promotionalSpace .container {width:600px; background-color:#ffffff; text-align:left; margin:0 auto; border:solid 1px #E9E9E9; }   
    #promotionalSpace #navigation { width:600px; height:30px; margin-bottom:20px; }
    #promotionalSpace #navigation .link { padding:5px; float:left; }
    #promotionalSpace h3 { font-size:16px; font-weight:bold; }
    #promotionalSpace h4 { font-size:15px; font-weight:bold; }
    #promotionalSpace h5 { font-size:14px; font-weight:bold; }
    #promotionalSpace h6 { font-size:13px; font-weight:bold; background-color:#E9E9E9; margin:5px; padding:5px; }
    #promotionalSpace .Success { color:#339900; background-color:#ccffcc; padding:2px; border:dashed 1px #339900; margin:10px; }
    #promotionalSpace .Error { color:Red; background-color:#ffffcc; padding:2px; border:dashed 1px red; margin:10px; }
    #promotionalSpace .Message { color:Blue; background-color:#D0EDFC; padding:2px; border:dashed 1px blue; margin:10px; }
    #promotionalSpace .button { padding:2px; margin:3px; border:solid 2px #A9D6FD; color:blue; background-color:#D2E7F9; text-decoration:none; display:inline-block; }
        #promotionalSpace .button:hover { color:blue; background-color:#A9D6FD; }
    #promotionalSpace #title { width:100%; height:30px; background-color:#EDF3FE; padding:10px 0; margin-bottom:10px; }
        #promotionalSpace #title h3 { color:#000; padding:5px; margin:0; }
    #promotionalSpace #content { width:100%;  }
        #promotionalSpace #content .control { width:100%; margin:3px; }
        #promotionalSpace #content .spacer { width:100%; height:20px; }
        #promotionalSpace #content .left { text-align:left; }

#promotionTemplate { width:156px; border:solid 1px #A9D6FD; float:left; margin:5px; padding:2px; background-color:#D2E7F9; text-align:center; }
    #promotionTemplate .box { width:154px; background-color:#ffffff; margin:0 auto; text-align:left; }
    #promotionTemplate .clear { clear:both; }
    #promotionTemplate .thumbnail { width:150px; height:150px; margin:2px; }
    #promotionTemplate .details { width:150px; padding:2px; }
        #promotionTemplate .details .control { width:100%; margin:3px; }

C# Enumerations with Default Value Strings

This extension class adds an extension method to the Enum type. This created the ability for each value of an Enumeration to have a default text value associated with it by simple adding an attribute to each enum value.

public enum MyEnum
{
[DisplayValue("#FFFFFF")]
White
}

static public class EnumExtension
    {
        ///
        /// Will get the string value for a given enums value, this will
        /// only work if you assign the StringValue attribute to
        /// the items in your enum.
        ///

        ///
        ///
        public static string GetDisplayValue(this Enum value)
        {
            // Get the type
            Type type = value.GetType();

            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            DisplayValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(DisplayValueAttribute), false) as DisplayValueAttribute[];

            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].DisplayValue : null;
        }
    }

///

/// This attribute is used to represent a string value

/// for a value in an enum.

///

public class DisplayValueAttribute : Attribute

{

#region Public Properties

///

/// Holds the stringvalue for a value in an enum.

///

public string DisplayValue { get; protected set; }

#endregion

#region Constructor

///

/// Constructor used to init a StringValue Attribute

///

///

public DisplayValueAttribute(string value)

{

DisplayValue = value;

}

#endregion

}


	

C# Recursively Find an ASP.net Control

Finds a control within the current page which is of a specific type and has a specific Id.

    public T FindControlById(string id) where T : Control
{
  return FindControlByTypeAndId(Page, id);
}

Finds a control within a specified ASP.net control which is of a specific type and has a specific Id.

    public static T FindControlByTypeAndId(Control startingControl, string id) where T : Control
    {
      // this is null by default
      T found = default(T);

      int controlCount = startingControl.Controls.Count;

      if (controlCount > 0)
      {
        for (int i = 0; i < controlCount; i++)
        {
          Control activeControl = startingControl.Controls[i];
          if (activeControl is T)
          {
            found = startingControl.Controls[i] as T;
            if (string.Compare(id, found.ID, true) == 0) break;
            found = null;
          }
          else
          {
            found = FindControlByTypeAndId(activeControl, id);
            if (found != null) break;
          }
        }
      }
      return found;
    }

Finds a list of controls within a specified ASP.net Control which are of a specific type

    public static List FindControlsByType(Control startingControl) where T : Control
    {
      // Container for storing all located controls of Type T
      List found = new List();

      //See how many controls are in the collection
      int controlCount = startingControl.Controls.Count;
      if (controlCount > 0)
      {
        for (int i = 0; i < controlCount; i++)
        {
          Control activeControl = startingControl.Controls[i];
          if (activeControl is T)
          {
            found.Add(startingControl.Controls[i] as T);
          }
          else
          {
            found.AddRange(FindControlsByType(activeControl));
          }
        }
      }
      return found;
    }

C# Custom Events in ASP.net Web Controls

First create an EventArgs class for your event. This is the class that will be passed through your event and holds values of interest to controls watching for the events fired by your custom control.

public class TemplateDisplayCommandEventArgs
{
#region Public Properties

public int SelectedTemplateId { get; private set; }

#endregion

#region Initializers

public TemplateDisplayCommandEventArgs(int templateId)
{
SelectedTemplateId = templateId;
}

#endregion
}

Then create the event handling delegate which will handle the passing of the EventArgs class through the event.

public delegate void TemplateDisplayCommandEventHandler(object sender, TemplateDisplayCommandEventArgs e);

Now within the code of your custom control add a public event of the EventHandler delegate type.

public event TemplateDisplayCommandEventHandler TemplateSelected;

Create a virtual method which will handle the calling of the event based on actions within the custom control.

protected virtual void OnTemplateSelected(TemplateDisplayCommandEventArgs e)
{
if (TemplateSelected != null) TemplateSelected(this, e);
}

Now from within your code you can simply call the virtual method when ever you want to fire the event.

OnTemplateSelected(new TemplateDisplayCommandEventArgs(Template.Id));

C# Dynamic Control Loading in ASP.net

First register the controls you will be dynamically loading in the .aspx/.ascx files.

With this registration block your dynamically created controls will appear as follows within the document.

<uc1:TemplateDisplay id=”Header1″
runat=”server”></uc1:
TemplateDisplay>

To add the controls dynamically use the code below to reference the control through its virtual path, cast it to its correct type, pass it its required parameters or set its properties. Then it is time to add it to a control collection, this can either be the control collection of the page or web user control you are developing or another ASP.net container control such as a PlaceHolder.

TemplateDisplay display = (TemplateDisplay) Page.LoadControl(“~/Library/Controls/TemplateDisplay.ascx”);
display.Template = template;
phTemplateDisplays.Controls.Add(display);