【问题标题】:Create dynamic check box and radio buttons in asp.net c#在asp.net c#中创建动态复选框和单选按钮
【发布时间】:2016-11-28 18:32:03
【问题描述】:
Basically I am creating a full dynamic form which has text boxes, check boxes, etc.

When I try to add this code in a page where `EnableViewState="false"` it doesn't work but it works fine on a page where `EnableViewState="true"`. 

But I want it to work on this (`EnableViewState="false"`) page. How do I do this?

这样做的基本想法是创建一个动态页面,只需单击一个按钮,我就可以在该页面上添加尽可能多的控件。控件可以重复。
面板 pnlTextBox; protected void Page_PreInit(object sender, EventArgs e) {

            //Create a Dynamic Panel
            pnlTextBox = new Panel();
            pnlTextBox.ID = "pnlTextBox";
            pnlTextBox.BorderWidth = 1;
            pnlTextBox.Width = 300;
            this.form1.Controls.Add(pnlTextBox);

            //Create a LinkDynamic Button to Add TextBoxes

            //Recreate Controls
            RecreateTextBoxControls("txtDynamic", "TextBox");
            RecreateDDLControls("ddlDynamic", "DropDownList");

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
   {
    //  /*  int i=Convert.ToInt32(DropDown1.SelectedValue.ToString());
    //    if (i == 1)
    //    {*/
          int cnt = FindOccurence("txtDynamic");
    CreateTextBox("txtDynamic-" + Convert.ToString(cnt + 1));
    //  /*  }
    //    else if (i == 2)
    //    {
    //        int cnt = FindOccurence("ddlDynamic");
    //        CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt + 1));
    //    }
    //    else if (i == 3)
    //    {

    //    }
    //    else if (i == 4)
    //    {

    //    }
    //    else if (i == 5)
    //    {
    //    }
    //    else if (i == 6)
    //    {

    //    }
    //    else
    //    {
    //        Console.Write("Bawa ji ka thullu");

    //    }
    //    */

    }
    private int FindOccurence(string substr)
    {
        string reqstr = Request.Form.ToString();
        return ((reqstr.Length - reqstr.Replace(substr, "").Length) /                                                                                                                                                        ....substr.Length);
    }
    private void RecreateTextBoxControls(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 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.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlTextBox.Controls.Add(ddl);

        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }
    private void CreateTextBox(string ID)
    {

        TextBox txt = new TextBox();
        txt.ID = ID;
        txt.AutoPostBack = true;
        txt.TextChanged += new EventHandler(OnTextChanged);
        pnlTextBox.Controls.Add(txt);

        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBox.Controls.Add(lt);
    }

【问题讨论】:

  • 那你为什么不保持真实呢?
  • 我想制作一个完整的动态表单,为此我需要添加所有控件,一些控件允许我使用真实状态,而另一些则使用错误状态。

标签: c# asp.net dynamic


【解决方案1】:

首先让我知道Ph是什么? 相同的代码对我有用。我所做的是创建了一个名为 Ph 的面板,然后将 CbxList 添加到该面板,即 Ph

这里是代码

*CheckBoxList CbxList = new CheckBoxList();
    TextBox txtBox = new TextBox();
    RadioButtonList rbList = new RadioButtonList();
    rbList.Items.Add("First Radio Button List");
    rbList.Items.Add("Second Radio Button List");
    rbList.Items.Add("Third Radio Button List");
    rbList.Items.Add("Fourth Radio Button List");
    rbList.Items.Add("Fifth Radio Button List");

    RadioButton rbTest = new RadioButton();
    rbTest.Text = "Simple Radio Button";
    txtBox.Text = "Simple Text Box";
    CbxList.ID = "Cbx";
    for (int i = 0; i < intCount; i++)
    {
        CbxList.Items.Add(new ListItem(Convert.ToChar(i + 65).ToString(), Convert.ToChar(i + 65).ToString()));
    }

    //Adding controls to Panel

    ph.Controls.Add(rbTest);

    ph.Controls.Add(CbxList);

    ph.Controls.Add(rbList);

    ph.Controls.Add(txtBox);

    ViewState["ListCreated"] = false;*

【讨论】:

  • 这是一个我们正在添加 chkbox 的面板。当我只添加 chkbox 时,它也适用于我,但在我的情况下,我需要在同一面板中添加 chkboxes 单选按钮文本框等。在这种情况下它不起作用。
  • 你能在这里分享你的代码吗?
  • 请看上面的代码。这对我有用。我认为缺少的是向面板添加控件。
  • 我在问题中添加了上面的代码,这是我的想法。感谢 Nasir Khan,如果你能帮我解决这个问题。
猜你喜欢
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 2014-06-14
  • 2013-08-03
相关资源
最近更新 更多