Pre-init function not finding all dynamic controls

大兔子大兔子 提交于 2019-12-19 11:44:11

问题


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.

来源:https://stackoverflow.com/questions/55817451/pre-init-function-not-finding-all-dynamic-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!