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

时间:2011-12-05 10:18:09

标签: c# asp.net

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

让我说我通过从下拉列表中选择选项来创建这样的控件。

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

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

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

这不是基于上面所选订单的正确订单。

这里的问题是设计不太正确。

当我重新创建控件时,我会重新创建符合其类型的控件:TextBoxes,然后DropDowns

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

<%@ 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));


        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么。您的代码显然一次创建了一种类型的所有控件,因此这是我期望在您的页面输出中看到的。

如果要交错控件,则需要将算法更改为逐个处理控件而不是按类型处理。