0
votes

my code is as below

public partial class newfields : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

    }
    protected void crtfields(object sender, System.EventArgs e)
    {
        int rowCnt;
        int rowCtr;
        Unit twidth = new Unit(600, UnitType.Pixel);
        Unit cwidth = new Unit(150, UnitType.Pixel);
        Unit cwidth1 = new Unit(300, UnitType.Pixel);
        Unit cwidth2 = new Unit(100, UnitType.Pixel);
        TableCell tcell1, tcell2, tcell3;
        rowCnt = int.Parse(tbxnofields.Text);
        for (rowCtr=1; rowCtr<=rowCnt; rowCtr++){
            TableRow tRow = new TableRow();
            tRow.Width = twidth;
            tblnoflds.Rows.Add(tRow);
            tcell1=new TableCell();
            tcell1.Controls.Add(new TextBox());
            tcell1.Width = cwidth1;
            tcell2=new TableCell();
            tcell2.Controls.Add(new TextBox());
            tcell2.Width = cwidth;
            tcell3 = new TableCell();
            tcell3.Controls.Add(new TextBox());
            tcell3.Width = cwidth2;
            tRow.Cells.Add(tcell1);
            tRow.Cells.Add(tcell2);
            tRow.Cells.Add(tcell3);
            tblnoflds.Rows.Add(tRow);
        }
    }
}

How can I assign an ID to the textbox I have set in the Controls.

1

1 Answers

0
votes

Instead of

tcell1.Controls.Add(new TextBox());

use something like

var t = new TextBox();
t.ClientIDMode = ClientIDMode.Static;
t.ClientID = "TextBox1"; //Set the ID here
tcell1.Controls.Add(t);

See here for documentation of the ClientIDMode property and how to use the ClientID.