我在从asp.net Table获取数据时遇到问题。它正确显示表,但是当我尝试从代码访问表时,我找不到表。这是我的代码。告诉我是否有其他方式做同样的事情。
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);
}
Panel1.Controls.Add(tbl);
}
调用函数获取响应
protected void btnSubmit_Click(object sender, EventArgs e)
{
List<suseranswer> surveyanswers = GetSurveyReponse();
foreach (suseranswer sres in surveyanswers)
{
context.AddTosuseranswers(sres);
Response.Write(sres.answer);
}
context.SaveChanges();
}
这是从表中获取数据的函数。它无法找到父控件(Panel1),但无法找到表格。
private List<suseranswer> GetSurveyReponse()
{
List<suseranswer> response = new List<suseranswer>();
foreach (Control ctr in Panel1.Controls)
{
if (ctr is Table)
{
Table tbl = ctr 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;
}
这是我的aspx文件的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="survey.aspx.cs" Inherits="csdatabaseModel.survey" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="vertical-align: middle; line-height: 15px; text-align: left; text-indent: 5px; background-color: #CCFFFF; border: 1px dashed #CCCCCC; padding: 10px; margin: 10px; table-layout: auto">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="btnSubmit" runat="server" Text="save" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
再次按照我的要求显示数据。
答案 0 :(得分:2)
当你在后面的代码上创建表时,这些数据不会保存在任何地方。
您可能会在页面上看到它们,但在回发后,这些数据永远不会在服务器和后面的代码上移回。
可能的解决方案。
答案 1 :(得分:0)
将表保存在Session Var中 这是本案的解决方案
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);
}
第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;
}