开发者

C# append to placeholder

开发者 https://www.devze.com 2022-12-21 19:20 出处:网络
So I am trying to make a dynamic form builder where people can add a new form and add fields to that form (the add field brings up several textboxes & dropdown options for that new field).Is there

So I am trying to make a dynamic form builder where people can add a new form and add fields to that form (the add field brings up several textboxes & dropdown options for that new field). Is there any way to append to a placeholder control when clicking 'add new field'? Also, what is the开发者_StackOverflow中文版 best way to get the values from those dynamically added controls?


A few simple steps... 1) Create a new instance of the control. Populate any desired properties. 2) Add it to the PlaceHolder using the PlaceHolder's .Controls.Add method. 3) Add the control's event handler. By using a delegate, as shown, you can access the control's values.

        DropDownList ddl = new DropDownList();
        ListItem li0 = new ListItem(string.Empty, "0");
        ListItem li1 = new ListItem("Hello", "1");
        ListItem li2 = new ListItem("World", "2");
        ddl.Items.Add(li0);
        ddl.Items.Add(li1);
        ddl.Items.Add(li2);
        ddl.AutoPostBack = true;
        PlaceHolder1.Controls.Add(ddl);
        ddl.SelectedIndexChanged += delegate(object snd, EventArgs evt) { DoSomething(ddl.SelectedValue); };




    public void DoSomething(string SelectedValue)
    {
        //Do something spectacular here...
    }


This is the other option (appending to a Literal Control). Each time the user clicks a button, a new field is created. I don't know how to add those dynamic fields to the database. Should I do a foreach?

protected void Button1_Click(object sender, EventArgs e)
{
    var thisstring = new StringWriter();
    var writer = new HtmlTextWriter(thisstring);
    var formLabel = new TextBox();
    formLabel.Text = idValue.ToString();
    writer.Write("Field Label:");
    formLabel.RenderControl(writer);
    var typeOptions = new DropDownList();
    typeOptions.DataSource = getfieldtypes();
    typeOptions.DataTextField = "description";
    typeOptions.DataValueField = "id";
    typeOptions.DataBind();
    writer.Write("Field Type:");
    typeOptions.RenderControl(writer);
    writer.WriteBreak();
    Literal1.Text += thisstring;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号