I have solved the issue. For this, I created the dynamic table and dynamic ASP.Net Controls from code behind. I solved it with the help of the Article explained by Mudassar Ahmed Khan which url is below: http://www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx.
My Code is given below:
.aspx Code:
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlDynamicControls" runat="server">
</asp:Panel>
<hr />
<asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetTextBoxValues" />
</div>
</form>
</body>
.aspx.cs Code:
SqlConnection strConnect = new SqlConnection(ConfigurationManager.AppSettings["SqlConn"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Show_Questions()
{
try
{
SqlDataAdapter da;
SqlCommand cmd;
string query = @"select distinct c.categoryID,c.CategoryName from Questionnaire_Question_Category c inner join Questionnaire_Questions q
on c.categoryID=q.categoryID where q.IsActive=1 and c.IsActive=1 ";
DataTable dtcat = new DataTable();
cmd = new SqlCommand(query, strConnect);
da = new SqlDataAdapter(cmd);
da.Fill(dtcat);
Table tbl = new Table();
tbl.ID = "myTable";
foreach (DataRow dr in dtcat.Rows)
{
TableRow tr = new TableRow();
TableCell td = new TableCell();
Label lblCat = new Label();
lblCat.ID = "lblCat" + dr["CategoryId"].ToString();
lblCat.Text = dr["CategoryName"].ToString();
lblCat.Attributes.Add("runat", "Server");
lblCat.EnableViewState = false;
td.Controls.Add(lblCat);
tr.Cells.Add(td);
tbl.Rows.Add(tr);
string query1 = @"select questionid,question,typeid from Questionnaire_Questions
where IsPublished=1 and categoryID=" + dr[0].ToString() + "";
DataTable dtqn = new DataTable();
cmd = new SqlCommand(query1, strConnect);
da = new SqlDataAdapter(cmd);
da.Fill(dtqn);
foreach (DataRow dr1 in dtqn.Rows)
{
TableRow tr2 = new TableRow();
TableCell td2 = new TableCell();
Label lblQuestion = new Label();
lblQuestion.Text = dr1["Question"].ToString();
lblQuestion.ID = "lblQstn" + dr1["QuestionId"].ToString();
lblQuestion.Attributes.Add("runat", "Server");
lblQuestion.EnableViewState = false;
td2.Controls.Add(lblQuestion);
tr2.Cells.Add(td2);
tbl.Rows.Add(tr2);
if (dr1[2].ToString() == "1")
{
TableCell td3 = new TableCell();
TextBox txt = new TextBox();
txt.ID =dr1["QuestionId"].ToString();
txt.Attributes.Add("runat", "Server");
// txt.Attributes.Add("OnFocusOut", "processText(" + a + ")");
txt.EnableViewState = false;
txt.Width = 200;
td3.Controls.Add(txt);
tr2.Cells.Add(td3);
//tbl.Rows.Add(tr2);
}
else if (dr1[2].ToString() == "2")
{
string query2 = @"select optionid,options from Questionnaire_Question_Options
where questionid=" + dr1[0].ToString() + "";
DataTable dtRop = new DataTable();
cmd = new SqlCommand(query2, strConnect);
da = new SqlDataAdapter(cmd);
da.Fill(dtRop);
TableRow tr3 = new TableRow();
foreach (DataRow dr2 in dtRop.Rows)
{
TableCell td3 = new TableCell();
RadioButtonList rbl = new RadioButtonList();
rbl.Items.Clear();
rbl.Attributes.Add("runat", "Server");
//rbl.ID = dr1["QuestionId"].ToString();
rbl.EnableViewState = false;
rbl.Items.Add(new ListItem { Text = dr2["Options"].ToString(), Value = dr2["OptionId"].ToString() });
td3.Controls.Add(rbl);
tr3.Cells.Add(td3);
}
tbl.Rows.Add(tr3);
}
else if (dr1[2].ToString() == "3")
{
string query3 = @"select optionid,options from Questionnaire_Question_Options
where questionid=" + dr1[0].ToString() + "";
DataTable dtCop = new DataTable();
cmd = new SqlCommand(query3, strConnect);
da = new SqlDataAdapter(cmd);
da.Fill(dtCop);
TableRow tr3 = new TableRow();
foreach (DataRow dr2 in dtCop.Rows)
{
TableCell td3 = new TableCell();
CheckBox chkb = new CheckBox();
chkb.Attributes.Add("runat", "Server");
chkb.EnableViewState = false;
//chkb.ID = dr1["QuestionId"].ToString();
chkb.Text = dr2[1].ToString();
td3.Controls.Add(chkb);
tr3.Cells.Add(td3);
}
tbl.Rows.Add(tr3);
}
}
// form1.Controls.Add(tbl);
pnlDynamicControls.Controls.Add(tbl);
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
Show_Questions();
}
protected void GetTextBoxValues(object sender, EventArgs e)
{
string messageText="";
string messageRadionBtnList="";
string messageCHkBox = "";
Table t = (Table)FindControl("pnlDynamicControls").FindControl("myTable");
foreach (TableRow tr in t.Rows)
{
foreach (TableCell tc in tr.Cells)
{
foreach (Control c in tc.Controls)
{
if (c is TextBox)
{
messageText += ((TextBox)c).Text + "\\n";
// To show Data using alert.
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + messageText + "');", true);
//To store Data in Database
QuestionnaireDTO objInsert = new QuestionnaireDTO();
objInsert.questionId = Convert.ToInt32(((TextBox)c).ID);
objInsert.textAnswers = ((TextBox)c).Text;
QuestionnaireBAL objInsertAnswers = new QuestionnaireBAL();
objInsertAnswers.Insert_TextAnswers(objInsert);
}
if (c is RadioButtonList)
{
messageRadionBtnList += ((RadioButtonList)c).SelectedValue + "\\n";
}
if (c is CheckBox)
{
messageCHkBox += ((CheckBox)c).Text + "\\n";
}
}
}
}
}