3

This is the control builder class...

public class ControlBuilder
{
    /// <summary>
    /// Html Control class for controlbuilder Control .
    /// </summary>
    protected HTMLControl formControl;

    /// <summary>
    /// Html Control class for the label.
    /// </summary>
    private HTMLControl labelControl;


    /// <summary>
    /// Getting the property for the Control .
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public HTMLControl Form
    {
        get { return formControl; }
    }

    /// <summary>
    /// Creating a label for the Control.
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public HTMLControl Label
    {
        get { return labelControl; }
    }

    /// <summary>
    /// Creating a construtor for the controlbuilder taking in Zero 
    /// arguments it creates a labl for the Control .
    /// </summary>
    /// <history>
    /// [LuckyR] 13/8/2009 Created
    /// </history>
    public ControlBuilder() { }

    /// <summary>
    /// A construtor for the controlbuilder which
    /// creates a label for the Control .
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public ControlBuilder(string labelName)
    {
        Label label = new Label();
        label.Text = labelName;
        label.Width= 200;
        labelControl = new HTMLControl(label);
    }


    /// <summary>
    /// Control build property that is used to biuld the Html 
    /// markup for the created Control.
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public string BuildControl()
    {
        this.CreateControl();
        this.SetAttribute();
        return this.RenderHTML();
    }

    /// <summary>
    /// Render Html tags for the Control with label . 
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public string RenderHTML()
    {
        return labelControl.RenderHTML() + ": " + formControl.RenderHTML();
    }

    /// <summary>
    /// Used to Set Attributes for the Control .
    /// </summary>
    /// <history>
    /// [LuckyR] 13/8/2009 Created
    /// </history>
    protected virtual void SetAttribute() { }

    /// <summary>
    /// Used to create the Control . 
    /// </summary>
    /// <history>
    /// [LuckyR] 13/8/2009 Created
    /// </history>
    protected virtual void CreateControl() { }

    /// <summary>
    /// A list of all the Controls that will be created during the 
    /// program run .
    /// </summary>
    private IList<ControlBuilder> Controls = new List<ControlBuilder>();

    /// <summary>
    /// A property to add Control to the ControlBuilder that are created by 
    /// the user.
    /// </summary>
    /// <history>
    /// [LuckyR] 13/8/2009 Created
    /// </history>
    /// <param name="Control">Controls from the controlbuilder class</param>
    public void AddControl(ControlBuilder Control)
    {
        Controls.Add(Control);
    }

    /// <summary>
    /// A property to display the Controls that are created by 
    /// the user.
    /// </summary>
    /// <history>
    /// [LuckyR] 13/8/2009 Created
    /// </history>
    public string Display()
    {
        string Html = string.Empty;

        foreach (ControlBuilder builder in Controls)
        {
            Html += builder.BuildControl();
            Html += "<br /><br />";
        }

        return Html;
    }
} 

}

this is how i build a control

public class TextBoxBuilder : ControlBuilder
{
    /// <summary>
    /// Creating a web Control textBox.
    /// </summary>
    private TextBox textBox;

    /// <summary>
    /// Creating an Id to add as an attribute .
    /// </summary>
    private string Id;

    /// <summary>
    /// Creating an Value to add as an attribute .
    /// </summary>
    private string Value; 

    /// <summary>
    /// Creating a Textbox constructor which takes in LabelName and Id. 
    /// to create a label for the Control. 
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    public TextBoxBuilder(string labelName, string id , string value): base(labelName)
    {
        this.Id = id;
        this.textBox = new TextBox();
        this.Value = value;
    }

    /// <summary>
    /// Used to Set properties for the Control . 
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    protected override void SetAttribute()
    {
        this.textBox.ID = this.Id;
        this.textBox.Text = this.Value;
    }

    /// <summary>
    /// Used to create the Control . That is done by calling the HtmlControl class 
    /// which inturn renders the particular Control for us .
    /// </summary>
    /// <history>
    /// [LuckyR] 10/8/2009 Created
    /// </history>
    protected override void CreateControl()
    {
        this.formControl = new HTMLControl(this.textBox);
    }
} 

}

In my home controller i did this ...

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

The view page looks like this ...

<% using (Html.BeginForm())
   {%>
<fieldset>
    <legend>Fields</legend>
    <p>
        <%= ViewData.Model.Display() %>
    </p>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
<% } %>
<div>
    <%=Html.ActionLink("Back to List", "Index")%>
</div>

since i am passing my class into the view i can retrieve all the data there with .display .

1 Answer 1

6

There is no concept of controls in ASP.NET MVC any longer.

You have two options:

  1. When the user clicks a button, you handle this POST request in a controller action, set some sort of flag in your view model to now show a textbox, then return the same view which in its turn will look at the flag and generate a text box if required. This will incur a complete round-trip which will be somewhat similar to the postback in WebForms.

  2. You do it in-place with JavaScript. You intercept a click event on a button and you inject an input/textarea HTML element into your document structure.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that it would be far easier in .NET MVC to create input areas like textboxes than webforms due to the problems with ids and controls. For that reason, I think the second solution is probably best.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.