如何在代码的其他部分中动态添加控件

本文关键字:动态 添加 控件 其他部 代码 | 更新日期: 2023-09-27 18:31:27

我有一个方法,可以根据用户必须输入的参数值的数量创建文本框。而且它工作正常。问题是,当用户单击"确定"时,我想获取用户在这些文本框中输入的值并将其替换为字符串。当我搜索这些文本框以从中获取文本值时,找不到它们。如何获取这些值以继续我的项目?代码为:

 protected void btnPreview_Click(object sender, EventArgs e)
    {
        lbHeader.Text = "Template Designer";
        divQueryBuilder.Visible = false;
        divTemplateDesigner.Visible = false;
        divFloating.Visible = true;
        if (txtQuery.Text.Contains("WHERE") || txtQuery.Text.Contains("where")||txtQuery.Text.Contains("Where"))
        {
            string[] splitter=new string[10];
            splitter[0]="Where";
            splitter[1]="WHERE";
            splitter[2]="where";
            splitter[3]="AND";
            splitter[4] = "and";
            splitter[5] = "And";
            string[] condition=txtQuery.Text.Split(splitter, StringSplitOptions.None);
            int numberOfParameters = condition.Length - 1;
            string[] Condition1 =null;
            Label[] newLabel = new Label[10];
            TextBox[] txtBox = new TextBox[10];
            for (int i = 0; i < numberOfParameters; i++)
            {
                string lbValue="lbValue";
                string lbID=lbValue+i;
                string txtValue = "txtValue";
                string txtID = txtValue + i;
                HtmlGenericControl genericControl = new HtmlGenericControl("br/");
                Condition1 = condition[i + 1].Split('[', ']');
                newLabel[i] = new Label();
                txtBox[i] = new TextBox();
                newLabel[i].ID=lbID;
                newLabel[i].Text = Condition1[1];
                txtBox[i].ID = txtID;
                td2.Controls.Add(newLabel[i]);
                td2.Controls.Add(genericControl);
                td2.Controls.Add(txtBox[i]);
                td2.Controls.Add(genericControl);
                txtBox[i].EnableViewState = true;
            }
        }
    }
 private bool ControlExistence(string lbID)
    {
        try
        {
            td2.FindControl(lbID);
            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
       // GetTextBoxValues();
        string[] splitter1 = new string[10];
        splitter1[0] = "Where";
        splitter1[1] = "WHERE";
        splitter1[2] = "where";
        splitter1[3] = "AND";
        splitter1[4] = "and";
        splitter1[5] = "And";
        string[] condition = txtQuery.Text.Split(splitter1, StringSplitOptions.None);
        int numberOfParameters = condition.Length - 1;
        string[] splitter = new string[4];
        splitter[0] = "[";
        splitter[1] = "]";
        splitter[2] = "'";
        string[] queryBoxValue = txtQuery.Text.Split(splitter,StringSplitOptions.RemoveEmptyEntries);
        StringBuilder query = new StringBuilder();
        TextBox txtBox = new TextBox();
        for (int i = 0; i < queryBoxValue.Length; i++)
        {
            if (!queryBoxValue[i].Contains("?"))
            {
                query.Append(queryBoxValue[i]);
                query.Append(" ");
            }
            else
            {
                for (int counter1 = 0; counter1 < numberOfParameters; counter1++)
                {
                    string txtValue = "txtValue";
                    string txtID = txtValue + counter1;
                    if (ControlExistence(txtID))
                    {
                        TextBox box = (TextBox)td2.FindControl(txtID);
                        string b = box.Text;
                        //txtBox.ID = txtID;
                    }
                    txtBox.Text = "'" + txtBox.Text + "'";
                    queryBoxValue[i] = queryBoxValue[i].Replace(queryBoxValue[i], txtBox.Text);
                    query.Append(queryBoxValue[i]);
                }
            }
        }
        string fireQuery = query.ToString();
        adp = new SqlDataAdapter(fireQuery, conn);
        tab = new DataTable();
        adp.Fill(tab);
        if (tab.Rows.Count > 0)
        {
            string[] tempString = txtTemplate.Text.Split(' ');
            for (int j = 0; j < tempString.Length; j++)
            {
                if (tempString[j].StartsWith("["))
                {
                    txtPreview.Text = txtTemplate.Text.Replace(tempString[j], tab.Rows[0][0].ToString());
                }
            }
        }
        divFloating.Visible = false;
        divQueryBuilder.Visible = false;
        divTemplateDesigner.Visible = true;
    }

请帮忙。这已经成为我的障碍。

如何在代码的其他部分中动态添加控件

列出添加的控件,并在需要时使用它。

        for (int i = 0; i < numberOfParameters; i++)
        {
            // ...
            td2.Controls.Add(newLabel[i]);
            td2.Controls.Add(genericControl);
            td2.Controls.Add(txtBox[i]);
            td2.Controls.Add(genericControl);
            addedTextBoxes.Add(txtBox[i]);
            // ...
        }

然后从代码的另一部分:

var values = addedTextBoxes.Select(tb => tb.Text).ToList();
foreach (var txtBox in addedTextBoxes)
{
    // ...
}

等。。。