Listing 1: HelloWorld implemented directly using MSIL

.assembly HelloWorld { }
.assembly extern mscorlib { }
.method public static void main() {
    .entrypoint
    ldstr "Hello .NET World"
    call void
        [mscorlib]System.Console::WriteLine
        (Class System.String)
    ret
}


Listing 2:  Adding controls directly to a System.Web.UI.Page controls collection
 causes a System.Web.HttpException to be thrown


/// <summary>
/// System.Web.HttpException thrown when attempt
/// ing to add controls
/// dynamically to the controls collection of a
/// System.Web.UI.Page
/// directly. Therefore we must add them to a
/// Panel instead.
/// </summary>
private void Page_Load(object sender, EventArgs e)
{
    try
    {
System.Web.UI.WebControls.TextBox _textbox
           = new System.Web.UI.WebControls.TextBox();

           textbox.Text = "Dynamically Inserted TextBox";

this.Controls.Add(_textbox); // will not render textbox!!!!
    }
    catch(System.Web.HttpException ex)
    {
     // System.Web.HttpException will occur if you
     // add controlsdirectly to the
     // System.Web.UI.Page controls collection.
    }
    catch(Exception ex)
    {
    }
}

Listing 3: Using a System.Web.UI.WebControls.Panel to add controls dynamically

protected System.Web.UI.WebControls SearchPanel
                = new System.Web.UI.WebControls();

/// <summary>
/// Creates a TextBox control dynamically and
/// successfully adds it
/// to the SearchPanel controls collection.
/// </summary>
private void Page_Load(object sender, EventArgs e)
{
    try
    {
System.Web.UI.WebControls.TextBox _textbox
           = new System.Web.UI.WebControls.TextBox();

            _textbox.Text = "Dynamically Inserted TextBox";

SearchPanel.Controls.Add(_textbox);
    }
    catch(System.Web.HttpException ex)
    {
     // What no worries ;)
    }
    catch(Exception ex)
    {
     // What no worries ;)
    }
}


Listing 4:  SearchNavigation.ascx Page_Load Event Handler

       private System.Type m_type = null;

private void Page_Load(object sender, EventArgs e)
{
    try
    {
      m_type = typeof(SearchPage);
      FieldInfo[] fields =
      m_type.GetFields(BindingsFlags.NonPublic |
            BindingsFlags.Instance);

               TableRow _row = new TableRow();

  foreach(FieldInfo field in fields)
  {
      if(field.FieldType.FullName
          == "System.Web.UI.WebControls.Panel")
           {
      LinkButton _linkButton = new LinkButton();
     linkButton.Text = field.Name.Replace("Panel",
     string.Empty);
     _linkButton.ForeColor = Color.FromArgb(99,
     99, 99);
     _linkButton.Click
     += new EventHandler(this.LinkButton_Clicked);

     TableCell _cell = new TableCell();
               _cell.Controls.Add(_linkButton);
               _row.Controls.Add(_cell);
                    }
  }
        }
    catch(Exception ex)
    {
        // What no worries ;)
    }
}


Listing 5:  SearchNavigation.ascx LinkButton_Clicked Event Handler

private void LinkButton_Clicked(object sender, EventArgs e)
{
            try
           {
               foreach(System.Web.UI.Control
               _control in this.Parent.Controls)
               {
                   if(_control.Type ==
				   typeof(System.Web.UI.WebControls.Panel))
                   {
                       if(_control.ID ==
                       ((LinkButton)sender).Text
                       + "Panel")
                          _control.Visible =
                          true;
                       else
                          _control.Visible =
                          false;
                   }
               }
           }
           catch(Exception ex)
           {
               // What no worries ;)
           }
       }


Listing 6

foreach(FieldInfo field in fields)
{
    if(field.FieldType.FullName ==
    "System.Web.UI.WebControls.Panel")
    {
               LinkButton _linkButton = new
               LinkButton();
               _linkButton.Text = field.Name.Re
               _place("Panel", string.Empty);
               _linkButton.ForeColor = Color.From
               Argb(99, 99, 99);
               _linkButton.Click
                           += new EventHandler(
                           this.LinkButton_
                           Clicked);

              TableCell _cell = new
               TableCell();
               _cell.Controls.Add(_linkButton);
               _row.Controls.Add(_cell);

    }
}


Listing 7:  SearchAttribute.cs

using System;

namespace PrecisionObjects.CustomAttributes
{
    /// <summary>
    /// class SearchableAttribute
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class SearchableAttribute : Attribute
    {
        private string m_validation;

        public SearchableAttribute(string validation)
        {
            m_validation = validation;
        }
    }
}


Listing 8: Publisher.cs

using System;
using PrecisionObjects.CustomAttributes;
using PrecisionObjects.Interfaces;

namespace PrecisionObjects.Organizations
{
    public class Publisher : ISearchable
    {
        private string m_name;
        private string m_city;
        private string m_state;
        private string m_country;
        private string m_contact;

        /// <summary>
        /// public string Name ~ gets / sets the
        /// publisher name
        /// </summary>
        [Searchable("Insert validation
        expression")]
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                m_name = value;
            }
        }

        /// <summary>
        /// public string City ~ gets / sets the
        /// publisher city
        /// </summary>
        [Searchable("Insert validation
        expression")]
        public string City
        {
            get
            {
                return m_city;
            }
            set
            {
                m_city = value;
            }
        }

        /// <summary>
        /// public string State ~ gets / sets the
        /// publisher state
        /// </summary>
        [Searchable("Insert validation
        expression")]
        public string State
        {
            get
            {
                return m_state;
            }
            set
            {
                m_state = value;
            }
        }

        /// <summary>
        /// public string Country ~ gets / sets
        /// the publisher country
        /// </summary>
        [Searchable("Insert validation
        expression")]
        public string Country
        {
            get
            {
                return m_country;
            }
            set
            {
                m_country = value;
            }
        }

        /// <summary>
        /// public string Contact ~ gets /
        /// sets the publisher contact
        /// </summary>
        public string Contact
        {
            get
            {
                return m_contact;
            }
            set
            {
                m_contact = value;
            }
        }

    }
}


Listing 9

=m_type = typeof(Publisher);

    // establish if m_type implements ISearchable
    if(m_type.IsClass && !m_type.IsAbstract)
    {
        m_interfaces = m_type.GetInterfaces();

        foreach(System.Type type in m_interfaces)
        {
                  if(type.Name == "ISearchable"
                  &&
                            type.Namespace ==
                            "PrecisionObjects.
                            Interfaces")
                  {

Listing 10

    PropertyInfo[] props = m_type.GetProperties();

    foreach(PropertyInfo p in props)
    {
        string name = p.Name;
           object[] supported
              = p.GetCustomAttributes(typeof
              (SearchableAttribute), false);

        if(supported.Length != 0) // Property Is
        Searchable
        {
            // Dynamically add associated controls
            // to ASP.NET table
           }
       }

Additional Code for this Article zip file ~7.61 KB