0
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?

Basic idea of doing this is to create a dynamic page on which I can add as many controls as I can with just clicking one button. Controls can repeat.
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 = 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);
    }
2
  • Why don't you keep it true then? Commented Jul 25, 2016 at 12:24
  • I want to make a complete dynamic form, for that purpose I need to add all controls, some controls allows me with true state and some are working with false state. Commented Jul 26, 2016 at 5:16

1 Answer 1

0

First of all let me know what Ph is? The same code works for me. What I did is I created a panel with the name Ph and then added the CbxList to that panel i.e. Ph

Here is the code

*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;*
Sign up to request clarification or add additional context in comments.

5 Comments

This is a panel in which we are adding chkbox. It also works for me when I only add chkbox but in my case i need to add chkboxes radio buttons textboxes etc in same panel. in this case it does't work.
Can you please share your code here so that I can get better idea?
Please have a look at the above code. This works for me. I think so what are missing is Adding Controls to the panel.
I have added the code above in the question which is my idea for doing this. Thanks Nasir Khan if you could help me out in this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.