繁体   English   中英

在按钮单击时查找动态生成的文本

[英]Find dynamically generated text on button click

我在我的项目中使用asp.net C#,我们在面板内动态生成了文本框,我想获取文本框的值,所以我使用了foreach循环来获取文本框,但没有得到文本框

在这里我生成控件

Table table = new Table();
table.ID = "table1";
table.Width = new Unit("100%");
table.BorderWidth = new Unit("1px");
table.CssClass = "tbl";

string Query = "SELECT * FROM XXCUS.MASTER_VERIFICATION";
OracleDataAdapter da = new OracleDataAdapter(Query, obj_Conn);

DataTable dt = new DataTable();
da.Fill(dt);

var Count = dt.Rows.Count;

if (Count > 0)
{
    TableHeaderCell cellheader = new TableHeaderCell();

    Label lblHeader = new Label();
    lblHeader.Text = "Select";

    Label lblDesc = new Label();

    TableHeaderRow thr = new TableHeaderRow();
    TableHeaderCell thc = new TableHeaderCell();
    TableHeaderCell thcode = new TableHeaderCell();
    TableHeaderCell thcReason = new TableHeaderCell();

    thc.Text = "Select";
    thcode.Text = "Description";
    thcReason.Text = "Reason";

    thr.Cells.Add(thc);
    thr.Cells.Add(thcode);
    //thr.CssClass = "pdlbl";

    thc.Width = new Unit("5px");

    thcode.Width = new Unit("75%");
    thcode.CssClass = "thcode";

    thcReason.Width = new Unit("20%");
    thcReason.CssClass = "thcReason";

    thr.Cells.Add(thcReason);
    table.Rows.Add(thr);

    for (int i = 0; i < Count; i++)
    {
            TableRow row = new TableRow();
            TableCell cellchk = new TableCell();
            TableCell celltxt = new TableCell();
            TableCell celldesc = new TableCell();

            TextBox txt = new TextBox();
            CheckBox chk = new CheckBox();
            txt.ID = "txt" + i.ToString();
            txt.MaxLength = 250;
            //txt.Width = new Unit("84%");
            //txt.CssClass = "txtCl";
            chk.ID = "chk" + i.ToString();
            txt.TextMode = TextBoxMode.MultiLine;
            cellchk.ID = "cellchk" + i.ToString();
            celltxt.ID = "celltxt" + i.ToString();
            cellchk.Controls.Add(chk);
            //chk.Attributes.Add("onClick", "alert('" + chk.ID + "')");
            Label lblDescription = new Label();
            lblDescription.Text = dt.Rows[i]["DESCRIPTION"].ToString();
            celldesc.Controls.Add(lblDescription);
            cellheader.Controls.Add(lblHeader);
            cellchk.Width = new Unit("5%");
            //cellchk.CssClass = "pd";
            celldesc.Width = new Unit("75%");
            //celldesc.CssClass = "pdlbl";
            celltxt.Width = new Unit("20%");
            celltxt.Controls.Add(txt);

            row.Cells.Add(cellchk);
            row.Cells.Add(celldesc);
            row.Cells.Add(celltxt);
            table.Rows.Add(row);
    }

    dvGenerateCntrl.Controls.Add(table);
}

我正在使用面板添加控件

<asp:Panel ID="dvGenerateCntrl" runat="server">
</asp:Panel>

我上面的代码看不到任何问题,因此问题可能出在如何将文本框控件添加到dvGenerateCntrol中。

您是否也可以显示此代码。

这是一些示例代码,其中将控件添加到表中的单元格,然后查找然后遍历行和单元格以查找文本框。

        Table t = new Table(); 
        TableRow tr = new TableRow(); 
        TableCell tc = new TableCell();
        System.Web.UI.WebControls.TextBox tb = new  System.Web.UI.WebControls.TextBox() { Text = "Testing" };

        tc.Controls.Add(tb); //Add Textbox to TableCell Control Collection
        tr.Cells.Add(tc); // Add TableCell to TableRow
        t.Rows.Add(tr); //Add TableRow to Table

        System.Web.UI.WebControls.Panel panel = new System.Web.UI.WebControls.Panel(); //This is your panel
        panel.Controls.Add(t); // Add Table to Panel

        Table tbl = panel.Controls.OfType<Table>().FirstOrDefault(); //Now to find the TextBox in the Table, in the Row, in the cell


        foreach (TableRow row in tbl.Rows) //Loop through each TableRow
        {
            foreach (TableCell cell in row.Cells)     //Loop through each TableCell
            {
                IEnumerable<System.Web.UI.WebControls.TextBox> txtBoxes = cell.Controls.OfType<System.Web.UI.WebControls.TextBox>(); //Search TableCell.Controls Collection for all TextBoxes
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM