【发布时间】:2015-10-01 23:40:55
【问题描述】:
我正在关注这个post
这是一个简单的应用程序,管理员可以在其中准备一份问卷和一份简单的调查,并与在我们网站上注册的任何人分享。
一旦最终用户完成调查,网站管理员(或任何被授权的人)可以分析调查结果和其他任何形式的反馈,如图形或文本。
--但是里面有些东西坏了--
当你添加问题时,你选择问题的类型,所以我做了这门课
public enum QuestionTypes
{
SingleLineTextBox, // will render a textbox
MultiLineTextBox, // will render a text area
YesOrNo, //will render a checkbox
SingleSelect, //will render a dropdownlist
MultiSelect //will render a listbox
}
并将其作为字符串保存在数据库中,但它在运行时以不同的方式呈现(文本框、SingleLineTextBox、YesOrNo 运行良好,但 MultiSelect 和 YesOrNo 不起作用)
此应用程序使用实体框架 - 有一个部分可以添加问题。它看起来像这样:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlTypes.Items.Add(QuestionTypes.SingleLineTextBox.ToString());
ddlTypes.Items.Add(QuestionTypes.MultiLineTextBox.ToString());
ddlTypes.Items.Add(QuestionTypes.SingleSelect.ToString());
ddlTypes.Items.Add(QuestionTypes.MultiSelect.ToString());
ddlTypes.Items.Add(QuestionTypes.YesOrNo.ToString());
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SurveyAppConString context = new SurveyAppConString();
Question quest = new Question();
quest.Text = txtTitle.Text.Trim();
quest.QuestionType = ddlTypes.SelectedItem.Text;
quest.Options = txtValues.Text.Trim();
context.AddToQuestions(quest);
context.SaveChanges();
}
之后,您可以将任何问题分配给调查,并且有一个页面可以显示所有调查。那里坏了。我想在运行时创建一个复选框并将他的值作为字符串保存在数据库中,并用列表框做同样的事情
这是示例代码(适用于文本框和下拉列表)
private void PopulateSurvey()
{
btnSubmit.Enabled = true;
List<Question> questions = (from p in context.Questions
join q in context.SurveyQuestions on p.ID equals q.QuestionID
where q.SurveyID == surveyid
select p).ToList();
Table tbl = new Table();
tbl.Width = Unit.Percentage(100);
TableRow tr;
TableCell tc;
TextBox txt;
CheckBox cbk;
DropDownList ddl;
foreach (Question q in questions)
{
tr = new TableRow();
tc = new TableCell();
tc.Width = Unit.Percentage(25);
tc.Text = q.Text;
tc.Attributes.Add("id", q.ID.ToString());
tr.Cells.Add(tc);
tc = new TableCell();
if (q.QuestionType.ToLower() == "singlelinetextbox")
{
txt = new TextBox();
txt.ID = "txt_" + q.ID;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
if (q.QuestionType.ToLower() == "multilinetextbox")
{
txt = new TextBox();
txt.ID = "txt_" + q.ID;
txt.TextMode = TextBoxMode.MultiLine;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
if (q.QuestionType.ToLower() == "singleselect")
{
ddl = new DropDownList();
ddl.ID = "ddl_" + q.ID;
ddl.Width = Unit.Percentage(41);
if (!string.IsNullOrEmpty(q.Options))
{
string[] values = q.Options.Split(',');
foreach (string v in values)
ddl.Items.Add(v.Trim());
}
tc.Controls.Add(ddl);
}
tc.Width = Unit.Percentage(80);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
pnlSurvey.Controls.Add(tbl);
}
你可以快速查看完整代码here这是一个数据库图:
注意—— 我在复选框区域工作并添加了类似的代码
我就是这样写代码的
if (q.QuestionType.ToLower() == "yesorno")
{
lblyes = new Label();
lblyes.Text = "yes";
tc.Controls.Add(lblyes);
cbk = new CheckBox();
cbk.ID = "cbk_" + q.ID;
//cbk.value = "true";
cbk.Width=Unit.Percentage(41);
tc.Controls.Add(cbk);
}
// On Postback|Save
sres.Response = (ctrc as CheckBox).Checked ? "yes" : "no";
效果很好(一个复选框)
制作两个复选框(最好创建一个单选按钮) 我创建并工作得很好
if (q.QuestionType.ToLower() == "yesorno")//this is the name you create it when add anew question type
{
lblyes = new Label();
lblyes.Text = "yes";
tc.Controls.Add(lblyes);
rbk = new RadioButton();
rbk.ID = "rbk_" + q.ID;
rbk.GroupName = "rbyesno";
rbk.Width = Unit.Percentage(41);
tc.Controls.Add(rbk);
//add another chexbox and label
lblno = new Label();
lblno.Text = "no";
tc.Controls.Add(lblno);
rbk = new RadioButton();
rbk.ID = "cbk_1" + q.ID;
rbk.GroupName = "rbyesno";
rbk.Width = Unit.Percentage(41);
tc.Controls.Add(rbk);
}
// On Postback|Save
else if (ctrc is RadioButton)
{
//sres.Response = (ctrc as CheckBox).Checked.ToString();
sres.Response = (ctrc as RadioButton).Checked ? "no" : "yes";
}
---现在我只需要创建一个列表来从表单中选择多个选项并将其作为字符串发送到数据库
--- 当我尝试添加 alistbox 时,coud 添加一个多重选择问题类型到 asurvey
这是代码的 asn-p
if (q.QuestionType.ToLower() == "MultiSelect")
{
lstmulti = new ListBox();
lstmulti.ID = "lst_" + q.ID;
lstmulti.Width = Unit.Percentage(41);
//lstmulti.Items.Add("on");
//lstmulti.Items.Add("sgsd");
//lstmulti.Items.Add("thre");
if (!string.IsNullOrEmpty(q.Options))
{
string[] values = q.Options.Split(',');
foreach (string v in values)
lstmulti.Items.Add(v.Trim());
}
tc.Controls.Add(lstmulti);
}
在保存中 else if (ctrc 是 ListBox) { //sres.Response = (ctrc as ListBox).SelectionMode.ToString(); sres.Response = (ctrc as ListBox).SelectedValue;
}
它并没有呈现为 alistbox
//在运行时创建列表 if (q.QuestionType.ToLower() == "MultiSelect") { 列表框 lstmulti = new ListBox(); lstmulti.ID = "lst_" + q.ID; lstmulti.Width = Unit.Percentage(41); lstmulti.Height = Unit.Percentage(100);
lstmulti.SelectionMode = ListSelectionMode.Multiple;
//to select multible choices and send to database
if (lstmulti.SelectionMode == ListSelectionMode.Multiple)
{
var selected = new List<string>();
for (int i = 0; i < lstmulti.Items.Count; i++)
{
if (lstmulti.Items[i].Selected)
selected.Add(lstmulti.Items[i].Text);
string selectedItem = lstmulti.Items[i].Text;
//insert command
///it should send the result to databse where the table name is Survey_Response and column is Response
//SurveyAppConString db=new SurveyAppConString();
// //db.Survey_Response.Include(m => m.Response) = string.Join(",", selected);
}
}
if (!string.IsNullOrEmpty(q.Options))
{
string[] values = q.Options.Split(',');
foreach (string v in values)
lstmulti.Items.Add(v.Trim());
}
tc.Controls.Add(lstmulti);
}
//在保存后
else if (ctrc is ListBox)
{
//sres.Response = (ctrc as ListBox).SelectionMode.ToString();
sres.Response = (ctrc as ListBox).Items.ToString();
}
notes :: 我正在使用带有实体框架的 asp.net webform
【问题讨论】:
标签: javascript c# asp.net webforms linq-to-entities