ASP.NET MVC:动态创建控件

3

这是控件生成器类...

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

这是我如何构建一个控件

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

在我的主控制器中,我这样做...

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

查看页面看起来像这样...
<% 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>

由于我将我的类传递到视图中,因此我可以使用.display在那里检索所有数据。

1个回答

6

ASP.NET MVC不再有控件的概念。

您有两个选择:

  1. 当用户单击按钮时,您可以在控制器操作中处理此POST请求,将某种标志设置为在视图模型中显示文本框,然后返回相同的视图,该视图将查看标志并在需要时生成文本框。这将产生完全的往返,与WebForms中的postback类似。

  2. 使用JavaScript直接完成。您可以拦截按钮上的点击事件,并将输入/文本区域HTML元素注入到文档结构中。


我认为在.NET MVC中创建像文本框这样的输入区域比WebForms更容易,因为存在ID和控件的问题。因此,我认为第二种解决方案可能是最好的。 - Dan Atkinson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接