ASP.net Dynamic Script Loading in HTML Head

To dynamically add script tags to the head section of an HTML page based on conditions at page load use the following code.

This one adds a link to an external .js file:

if(UseUrchin)
      {
        HtmlGenericControl Include = new HtmlGenericControl("script");
        Include.Attributes.Add("type", "text/javascript");
        Include.Attributes.Add("src", "/__utm.js");
        Page.Header.Controls.Add(Include);
      }

This one adds actual JavaScript code inside the script tag:

if(UseUrchin)
      {
        HtmlGenericControl Include = new HtmlGenericControl("script");
        Include.Attributes.Add("type", "text/javascript");
        Include2.InnerHtml = "alert('We added it to the head');";       
Page.Header.Controls.Add(Include);
      }

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);