Pre-init function not finding all dynamic controls

2020-03-31 08:15发布

问题:

My scenario : I have 3 radio button. when clicked each one of them show different set of textboxes and a button are created dynamically. My Problem is when i clicked on the button i lose all the dynamically created controls.

i eventually found out to initialize the dynamic control on Page_Preinit as below.

  protected void Page_PreInit(object sender, EventArgs e)

    {



        List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
        int i = 1;
        foreach (string key in keys)
        {
            TextBox CPDT = new TextBox();

            CPDT.ID = "test" + i.ToString();

            CPDT.CssClass = "form-control";

            Label lblCPD = new Label();

            lblCPD.ID = "txtDynamiclbl" + "test" + i.ToString();

            lblCPD.CssClass = "form-control-label";

            lblCPD.Text = textbox[i].ToString();


            CPDPlaceHolder.Controls.Add(lblCPD);

            CPDPlaceHolder.Controls.Add(CPDT);

            i++;
        }
           Button callSoap = new Button();

            callSoap.ID = "txtDynamicSearch" + servicename;

            callSoap.Text = "Search";

            callSoap.CssClass = ".btn-info";

            callSoap.CommandArgument = "test";

            callSoap.Click += new EventHandler(btnsoap);

            callSoap.EnableViewState = true;

            CPDPlaceHolder.Controls.Add(callSoap);
    }

The Problem is with the first Line,its only finding the button ID and not the textbox ID. I can't seem to figure out what to do,can anyone help.