I think you need to modify your show method like this to have the controls inside placeholder, if your datatable
is returning multiple rows you need to loop through all the rows to get the data of individual
public void show()
{
dt = g1.return_dt("select product1,product2,product3,product4,product5,name from tbl_products where name='Yo Yo'");
if (dt.Rows.Count > 0)
{
txtname.Text = dt.Rows[0]["name"].ToString();
txtt1.Text = dt.Rows[0]["product1"].ToString();
//TextBox txtb;
int x = 2;
if (x <= 5)
{
if (Counter <= 4)
{
TextBox tb = new TextBox();
Counter++;
tb.ID = "TextBox" + Counter;
//tb.Text = tb.ID;
LiteralControl linebreak = new LiteralControl("<br />");
tb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
PlaceHolder2.Controls.Add(tb);
PlaceHolder2.Controls.Add(linebreak);
ControlIdList.Add(tb.ID);
ViewState["ControlIdList"] = ControlIdList;
x++;
}
//txtb = (TextBox)ctrl;
//LiteralControl linebreak = new LiteralControl("<br />");
//txtb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
//PlaceHolder2.Controls.Add(txtb);
//PlaceHolder2.Controls.Add(linebreak);
//x++;
}
}
}
If it was fixed that the controls should not be more that 4 try this code, while your clicking on showall
replace with DataTable
row as per your requirement
protected void Button3_Click(object sender, EventArgs e)
{
createControls();
}
private void createControls()
{
PlaceHolder2.Controls.Clear();
for (int i = 0; i < 4; i++)
{
TextBox tb = new TextBox();
tb.ID = "TextBox" + i;
tb.Text = tb.ID;
LiteralControl linebreak = new LiteralControl("<br />");
PlaceHolder2.Controls.Add(tb);
PlaceHolder2.Controls.Add(linebreak);
}
}
I have created a datatable as per your requirement check this
private void createControls()
{
PlaceHolder2.Controls.Clear();
DataTable dt = Session["Table"] as DataTable;
for (int i = 1; i < 5; i++)
{
int cnt = 0;
cnt = i;
TextBox tb = new TextBox();
cnt = cnt + 1;
tb.ID = "TextBox" + i;
tb.Text = dt.Rows[0]["Product" + cnt + ""].ToString();
LiteralControl linebreak = new LiteralControl("<br />");
PlaceHolder2.Controls.Add(tb);
PlaceHolder2.Controls.Add(linebreak);
}
}
private void assignValues()
{
DataTable dt = new DataTable();
dt.Columns.Add("Product2", typeof(string));
dt.Columns.Add("Product3", typeof(string));
dt.Columns.Add("Product4", typeof(string));
dt.Columns.Add("Product5", typeof(string));
DataRow lrow = dt.NewRow();
lrow["Product2"] = "ABC";
lrow["Product3"] = "DEF";
lrow["Product4"] = "GHI";
lrow["Product5"] = "JKL";
dt.Rows.Add(lrow);
Session["Table"] = dt;
}
protected void Button3_Click(object sender, EventArgs e)
{
assignValues();
createControls();
}
Here is the o/p as per my datatable on clicking showall