动态控制创建中的设计问题

本文关键字:问题 创建 动态控制 | 更新日期: 2023-09-27 18:16:36

我正在创建一个动态控件,我有一个下拉菜单选项"文本框,复选框,下拉框,标签"当用户从下拉菜单中选择任何这些选项时,控件被创建

假设我通过从下拉菜单中选择选项来创建这样的控件。

1: text box 
2: drop down 
3: text box:
4: dropdown

但是在页面输出中显示如下:

1: textbox
2: textbox
3: dropdown
4: dropdown

根据上面选择的顺序,这不是正确的顺序。

问题是这个设计不太对。

当我重新创建控件时,我根据它们的类型重新创建控件:文本框,然后下拉菜单

请找到附加的代码,任何建议如何解决这个问题将是有帮助的。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dynamiccontrol.aspx.cs" Inherits="Dynamic_controls.Dynamiccontrol" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlcontrols" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlDynamic_SelectedIndexChanged">
            <asp:ListItem>--Select--</asp:ListItem>
            <asp:ListItem>Textbox</asp:ListItem>
            <asp:ListItem>Dropdown</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

In.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Dynamic_controls
{
    public partial class Dynamiccontrol : System.Web.UI.Page
    {
        Panel pnlTextBox;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            //Create a Dynamic Panel
            pnlTextBox = new Panel();
            pnlTextBox.ID = "pnlTextBox";
            pnlTextBox.BorderWidth = 1;
            pnlTextBox.Width = 800;
            pnlTextBox.Height = 800;
            this.form1.Controls.Add(pnlTextBox);
            RecreateTxtControls("txtDynamic", "TextBox");
            RecreateDDLControls("ddlDynamic", "DropDownList");
            RecreateChkControls("chkDynamic", "CheckBox");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private int FindOccurence(string substr)
        {
            string reqstr = Request.Form.ToString();
            return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
        }
    private int FindOccurenceCheckbox(string substr)
    {
        string reqstr = Request.Form.ToString();
        substr = "chkDynamic";
        return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
    }
    private int FindOccurenceLabel(string substr)
    {
        string reqstr = Request.Form.ToString();
        substr = "lblDynamic";
        return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
    }

    private void RecreateTxtControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];
                        if (ctrlType == "TextBox")
                        {
                            CreateTextBox(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }
    private void RecreateDDLControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];
                        if (ctrlType == "DropDownList")
                        {
                            CreateDropDownList(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }
    private void RecreateChkControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];
                        if (ctrlType == "CheckBox")
                        {
                            CreateCheckbox(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }
    private void ReqFieldValidator(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];
                        if (ctrlType == "RequiredFieldValidator")
                        {
                            CreateCheckbox(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }
    private void ReqLabel(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&');
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];
                        if (ctrlType == "Label")
                        {
                            CreateLabel(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }

    private void CreateLabel(string ID)
    {
        Label lbl = new Label();
        lbl.ID = ID;
        lbl.Text = "text";
        pnlTextBox.Controls.Add(lbl);
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }
    private void CreateTextBox(string ID)
    {
        //CreateLabel(ID);
        TextBox txt = new TextBox();
        txt.ID = ID;
        pnlTextBox.Controls.Add(txt);
        // txt.AutoPostBack = true;
        // txt.TextChanged += new EventHandler(OnTextChanged);
        RequiredFieldValidator req = new RequiredFieldValidator();
        req.ControlToValidate = txt.ID;
        req.EnableViewState = false;
        pnlTextBox.Controls.Add(txt);
        Literal lt = new Literal();
        lt.Text = "<br />";
    }

    private void CreateCheckbox(string ID)
    {
        CheckBox chk = new CheckBox();
        chk.ID = ID;
        chk.Checked = true;
        chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
        chk.AutoPostBack = true;
        chk.EnableViewState = false;
        pnlTextBox.Controls.Add(chk);
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }
    void chk_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
        }
        catch (Exception ex)
        {
        }
    }
    private void CreateDropDownList(string ID)
    {
        DropDownList ddl = new DropDownList();
        ddl.ID = ID;
        ddl.Items.Add(new ListItem("--Select--", ""));
        ddl.Items.Add(new ListItem("One", "1"));
        ddl.Items.Add(new ListItem("Two", "2"));
        ddl.Items.Add(new ListItem("Three", "3"));
        // ddl.AutoPostBack = true;
        ddl.EnableViewState = false;
        // ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlTextBox.Controls.Add(ddl);
        //int cnt2 = FindOccurence("CheckBox");
        //CreateCheckbox("CheckBox-" + Convert.ToString(cnt2 + 1));
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }
    private void CreateRequiredFieldValidator(string ID)
    {
        RequiredFieldValidator req = new RequiredFieldValidator();
        req.ID = ID;
        // ddl.AutoPostBack = true;
        // ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlTextBox.Controls.Add(req);
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }
    protected void OnTextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        string ID = txt.ID;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type = 'text/javascript'>alert('" + ID + " fired OnTextChanged event');</script>");
        //Place the functionality here
    }
    protected void ddlDynamic_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlcontrols.SelectedValue == "Textbox")
        {
            int cnt = FindOccurence("txtDynamic");
            CreateTextBox("txtDynamic-" + Convert.ToString(cnt + 1));
            int cntChk = FindOccurenceCheckbox("CheckBoxdll");
            CreateCheckbox("chkDynamic-" + Convert.ToString(cntChk + 1));
        }
        if (ddlcontrols.SelectedValue == "Dropdown")
        {
            int cnt = FindOccurence("ddlDynamic");
            CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt + 1));
            int cntChk = FindOccurenceCheckbox("chkDynamic");
            CreateCheckbox("chkDynamic-" + Convert.ToString(cntChk + 1));

        }
    }
}
}

动态控制创建中的设计问题

我不知道你的问题是什么。你的代码显然是一次创建一种类型的所有控件,所以这就是我希望在你的页面输出中看到的。

如果你想交错的控件,那么你需要改变你的算法,一个一个地处理控件,而不是按类型。