简体   繁体   中英

Find dynamically generated text on button click

I'm using asp.net C# in my project we have dynamically generated textbox inside panel I want to get textbox value,so I'm using the foreach loop for getting the Textbox but I'm not getting the textbox

Here I generate the control

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);
}

and I'm using the panel for add the control

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

I don't see any issues with your above code so maybe the issue is how you adding the textbox control to dvGenerateCntrol.

Can you show this code as well.

Here is some example code of adding a control to a cell within a table and then finding then iterating through the rows and cells to find the textbox.

        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
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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