Save the table in Session Var
This is the solution for this case
private void PopulateSurvey()
{
btnSubmit.Enabled = true;
List<squestion> questions = (from p in context.squestions
where p.surveyid == surveyid
select p).ToList();
Table tbl = new Table();
tbl.Attributes.Add("id", "datatable");
tbl.Width = Unit.Percentage(100);
TableRow tr;
TableCell tc;
TableCell counter;
TextBox txt;
CheckBoxList cbk;
DropDownList ddl;
RadioButtonList ddl1;
int i = 1;
foreach (squestion q in questions)
{
tr = new TableRow();
counter = new TableCell();
counter.Text = i.ToString() + " ";
tc = new TableCell();
tc.Width = Unit.Percentage(25);
tc.Text = q.qtext;
tc.Attributes.Add("id", q.qid.ToString());
i++;
tr.Cells.Add(counter);
tr.Cells.Add(tc);
tc = new TableCell();
if (q.qtype.ToLower() == "radiobutton")
{
ddl1 = new RadioButtonList();
ddl1.ID = "ddl_" + q.surveyid;
ddl1.Width = Unit.Percentage(41);
List<sanswer> sanswers = (from z in context.sanswers
where z.qid == q.qid
select z).ToList();
foreach (sanswer ans in sanswers)
{
ddl1.Items.Add(ans.anstext);
}
tc.Controls.Add(ddl1);
}
else if (q.qtype.ToLower() == "checkbox")
{
cbk = new CheckBoxList();
cbk.ID = "ddl_" + q.surveyid;
cbk.Width = Unit.Percentage(41);
List<sanswer> sanswers = (from z in context.sanswers
where z.qid == q.qid
select z).ToList();
foreach (sanswer ans in sanswers)
{
cbk.Items.Add(ans.anstext);
}
tc.Controls.Add(cbk);
}
else if (q.qtype.ToLower() == "singlelinetextbox")
{
txt = new TextBox();
txt.ID = "txt_" + q.surveyid;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
else if (q.qtype.ToLower() == "multilinetextbox")
{
txt = new TextBox();
txt.ID = "txt_" + q.surveyid;
txt.TextMode = TextBoxMode.MultiLine;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
else if (q.qtype.ToLower() == "singleselect")
{
ddl = new DropDownList();
ddl.ID = "ddl_" + q.surveyid;
ddl.Width = Unit.Percentage(41);
List<sanswer> sanswers = (from z in context.sanswers
where z.qid == q.qid
select z).ToList();
foreach (sanswer ans in sanswers)
{
ddl.Items.Add(ans.anstext);
}
tc.Controls.Add(ddl);
}
tc.Width = Unit.Percentage(80);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
Session["Tabla"] = tbl;
Panel1.Controls.Add(tbl);
}
Part 2
private List<suseranswer> GetSurveyReponse()
{
List<suseranswer> response = new List<suseranswer>();
foreach (Control ctr in Panel1.Controls)
{
if (Session["Tabla"] is Table)
{
Table tbl = Session["Tabla"] as Table;
foreach (TableRow tr in tbl.Rows)
{
suseranswer sres = new suseranswer();
sres.suserid = "anyone";
sres.qid = tr.Cells[0].Attributes["ID"];
TableCell tc = tr.Cells[1];
foreach (Control ctrc in tc.Controls)
{
if (ctrc is TextBox)
{
sres.answer = (ctrc as TextBox).Text.Trim();
}
else if (ctrc is DropDownList)
{
sres.answer = (ctrc as DropDownList).SelectedValue;
}
else if (ctrc is RadioButtonList)
{
sres.answer = (ctrc as DropDownList).SelectedValue;
}
else if (ctrc is CheckBoxList)
{
foreach (ListItem item in (ctrc as CheckBoxList).Items)
{
if (item.Selected)
{
string selectedValue = item.Value.ToString();
sres.answer = selectedValue + "%";
}
}
}
}
response.Add(sres);
}
}
}
return response;
}