IN Aspx Page :
<div>
<table cellpadding="0" cellspacing="0" border="0" id="tblQuestions" runat="server">
</table>
</div>
In Aspx.cs Page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
namespace Practice.Aspx
{
public partial class BuildDynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
BuildDynamicTextBoxesForQuestions();
}
catch (Exception ex)
{
}
}
private void BuildDynamicTextBoxesForQuestions()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("Question", typeof(string));
dt.Rows.Add("Question1");
dt.Rows.Add("Question2");
dt.Rows.Add("Question3");
dt.Rows.Add("Question4");
dt.Rows.Add("Question5");
dt.Rows.Add("Question6");
dt.Rows.Add("Question7");
dt.Rows.Add("Question8");
if (dt != null && dt.Rows.Count > 0)
{
HtmlTable tbl = null;
tbl = (HtmlTable)this.FindControl("tblQuestions");
if (tbl != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlTableRow newRow = new HtmlTableRow();
HtmlTableCell newCell1 = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = dt.Rows[i]["Question"].ToString();
lbl.ID = "lbl" + i.ToString();
newCell1.Controls.Add(lbl);
newRow.Cells.Add(newCell1);
HtmlTableCell newCell2 = new HtmlTableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt" + i.ToString();
newCell2.Controls.Add(txtBox);
newRow.Cells.Add(newCell2);
tbl.Rows.Add(newRow);
}
}
}
}
catch (Exception ex)
{
}
}
}
}