Aaditya Ganesan

Aaditya Ganesan

  • NA
  • 40
  • 46.8k

How to bind textboxes dynamically using C#

Jan 24 2011 4:58 PM
Hi,

I have created a table of textboxes dynamically with a constant 8 column but the row value keeps changing with the corresponding selection from the dropdown.

I want to fill these textboxes as they are created dynamically from SQL database,
but I am not sure on how to refer them in order to bind them with the database.
I ll attach a part of the code here. plz look into it and help me.


PlaceHolder1.Controls.Clear();
Table table = new Table();

table.ID = "Table1";
PlaceHolder1.Controls.Add(table);
// Now iterate through the table and add your controls
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 8; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox ();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
// Add the control to the TableCell
SqlConnection cnn = new SqlConnection("Data Source=MPMSQLDEV;nitial Catalog=ASN;User ID=sa;Password=marcal");
cnn.Open();
SqlCommand cmmd = new SqlCommand("SELECT polinenum,marcalitem, vendornum, itemdesc, qtyunit, noroll, totqty, unit FROM asnvendor WHERE (vendorno ='" + TextBox116.Text + "') AND (ponumber ='" + TextBox117.Text + "')", cnn);

SqlDataReader dar = cmmd.ExecuteReader();
while (dar.Read())
{
//???????????????????????????????????????????????????????????????????????

}

cnn.Close();
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}

I hope I am clear, pls let me know the possible answer, the 8 columns are bound to different fields as per the select statement and the following number of rows are bound to the same fields of the as per the columns...


Thanks in advance
Aaditya


Answers (4)