title: ASP.NET MVC: creating controls dynamically
body: <p>This is the control builder class...</p>
  
  <pre><code>public class ControlBuilder
  {
      /// &lt;summary&gt;
      /// Html Control class for controlbuilder Control .
      /// &lt;/summary&gt;
      protected HTMLControl formControl;
  
      /// &lt;summary&gt;
      /// Html Control class for the label.
      /// &lt;/summary&gt;
      private HTMLControl labelControl;
  
  
      /// &lt;summary&gt;
      /// Getting the property for the Control .
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public HTMLControl Form
      {
          get { return formControl; }
      }
  
      /// &lt;summary&gt;
      /// Creating a label for the Control.
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public HTMLControl Label
      {
          get { return labelControl; }
      }
  
      /// &lt;summary&gt;
      /// Creating a construtor for the controlbuilder taking in Zero 
      /// arguments it creates a labl for the Control .
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 13/8/2009 Created
      /// &lt;/history&gt;
      public ControlBuilder() { }
  
      /// &lt;summary&gt;
      /// A construtor for the controlbuilder which
      /// creates a label for the Control .
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public ControlBuilder(string labelName)
      {
          Label label = new Label();
          label.Text = labelName;
          label.Width= 200;
          labelControl = new HTMLControl(label);
      }
  
  
      /// &lt;summary&gt;
      /// Control build property that is used to biuld the Html 
      /// markup for the created Control.
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public string BuildControl()
      {
          this.CreateControl();
          this.SetAttribute();
          return this.RenderHTML();
      }
  
      /// &lt;summary&gt;
      /// Render Html tags for the Control with label . 
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public string RenderHTML()
      {
          return labelControl.RenderHTML() + ": " + formControl.RenderHTML();
      }
  
      /// &lt;summary&gt;
      /// Used to Set Attributes for the Control .
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 13/8/2009 Created
      /// &lt;/history&gt;
      protected virtual void SetAttribute() { }
  
      /// &lt;summary&gt;
      /// Used to create the Control . 
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 13/8/2009 Created
      /// &lt;/history&gt;
      protected virtual void CreateControl() { }
  
      /// &lt;summary&gt;
      /// A list of all the Controls that will be created during the 
      /// program run .
      /// &lt;/summary&gt;
      private IList&lt;ControlBuilder&gt; Controls = new List&lt;ControlBuilder&gt;();
  
      /// &lt;summary&gt;
      /// A property to add Control to the ControlBuilder that are created by 
      /// the user.
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 13/8/2009 Created
      /// &lt;/history&gt;
      /// &lt;param name="Control"&gt;Controls from the controlbuilder class&lt;/param&gt;
      public void AddControl(ControlBuilder Control)
      {
          Controls.Add(Control);
      }
  
      /// &lt;summary&gt;
      /// A property to display the Controls that are created by 
      /// the user.
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 13/8/2009 Created
      /// &lt;/history&gt;
      public string Display()
      {
          string Html = string.Empty;
  
          foreach (ControlBuilder builder in Controls)
          {
              Html += builder.BuildControl();
              Html += "&lt;br /&gt;&lt;br /&gt;";
          }
  
          return Html;
      }
  } 
  </code></pre>
  
  <p>}</p>
  
  <p>this is how i build a control</p>
  
  <pre><code>public class TextBoxBuilder : ControlBuilder
  {
      /// &lt;summary&gt;
      /// Creating a web Control textBox.
      /// &lt;/summary&gt;
      private TextBox textBox;
  
      /// &lt;summary&gt;
      /// Creating an Id to add as an attribute .
      /// &lt;/summary&gt;
      private string Id;
  
      /// &lt;summary&gt;
      /// Creating an Value to add as an attribute .
      /// &lt;/summary&gt;
      private string Value; 
  
      /// &lt;summary&gt;
      /// Creating a Textbox constructor which takes in LabelName and Id. 
      /// to create a label for the Control. 
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      public TextBoxBuilder(string labelName, string id , string value): base(labelName)
      {
          this.Id = id;
          this.textBox = new TextBox();
          this.Value = value;
      }
  
      /// &lt;summary&gt;
      /// Used to Set properties for the Control . 
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      protected override void SetAttribute()
      {
          this.textBox.ID = this.Id;
          this.textBox.Text = this.Value;
      }
  
      /// &lt;summary&gt;
      /// Used to create the Control . That is done by calling the HtmlControl class 
      /// which inturn renders the particular Control for us .
      /// &lt;/summary&gt;
      /// &lt;history&gt;
      /// [LuckyR] 10/8/2009 Created
      /// &lt;/history&gt;
      protected override void CreateControl()
      {
          this.formControl = new HTMLControl(this.textBox);
      }
  } 
  </code></pre>
  
  <p>}</p>
  
  <p>In my home controller i did this ...</p>
  
  <pre><code>      public ActionResult Create()
        {
           ///Where i am contacting the linq to sql classs for performing ths operagtion
           foreach (var control in Rep.GetData(ScreenName))
           {
              string Type = control.Type;
              string value = null;
              if (id != Guid.Empty)
              {
                  value = DataObj.GetValue(control.TableName, control.ControlName, id);
              }
  
              switch (Type)
              {
                  case ("TextBox"):
                      /// Buliding a textBox box 
                      controlbuilder.AddControl(new TextBoxBuilder(control.Field, control.ControlName, value));
                      break;
  
                  case ("CheckBox"):
                      /// Bulidig a CheckBox .
                      controlbuilder.AddControl(new CheckBoxBuilder(control.Field, control.ControlName , value));
                      break;
  
                  case ("DatePicker"):
                      /// Bulidig a DatePicker .
                      controlbuilder.AddControl(new DatePicker(control.Field, control.ControlName, value));
                      break;
  
                  case ("DropDownList"):
                      ///Building a dropdownlist.
                      List&lt;string&gt; list = DataObj.GetDropDownValues(control.Id);
                      controlbuilder.AddControl(new DropDownListBuilder(control.Field, control.ControlName, list,value));
                      break;
  
                  case ("TextArea"):
                      /// Building a textBox area .
                      controlbuilder.AddControl(new TextArea(control.Field, control.ControlName , value));
                      break;
  
                  default:
                      break;
              }
          }
  
          return View(controlbuilder);
         }
  </code></pre>
  
  <p>The view page looks like this ...</p>
  
  <pre><code>&lt;% using (Html.BeginForm())
     {%&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;Fields&lt;/legend&gt;
      &lt;p&gt;
          &lt;%= ViewData.Model.Display() %&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Create" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  &lt;% } %&gt;
  &lt;div&gt;
      &lt;%=Html.ActionLink("Back to List", "Index")%&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>since i am passing my class into the view i can retrieve all the data there with .display .</p>