在 运行 时间内创建一个 c# listBox 到 select 多个选择(来自数据库)
create a c# listBox in run time to select multiple choices (that comes from database)
我正在关注这个 post
这是一个简单的应用程序,管理员可以在其中准备问卷和简单的调查,并与在我们网站上注册的任何人共享。
最终用户完成调查后,网站管理员(或任何获得授权的人)可以分析调查结果和其他任何形式的反馈,例如图形或文本。
-- 但是里面有些东西坏了--
添加问题时选择问题类型,所以我做了这个class
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 不工作)
此应用程序使用 entity framework - 有一个部分可以添加问题。它看起来像这样:
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";
它是这样显示的
它工作正常(一个复选框)
做两个checkbox(最好做一个radiobutton)
我创造并工作得很好
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";
}
我们检查最后一个单选按钮(在第一个单选按钮之后创建)
--- 现在我只需要创建一个列表 select 从表单中选择多个选项并将其作为字符串发送到数据库
--- 当我尝试添加 alistbox 时 colud 添加一个 multible select quesion type to asurvey
这是代码片段
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
//在运行 时间内创建列表
如果 (q.QuestionType.ToLower() == "MultiSelect")
{
ListBox 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);
}
//在post-保存
else if (ctrc is ListBox)
{
//sres.Response = (ctrc as ListBox).SelectionMode.ToString();
sres.Response = (ctrc as ListBox).Items.ToString();
}
注意事项 :: 我正在使用 asp.net 带有实体框架的网络表单
您可以轻松地按照示例添加一个具有 "true" 值的复选框控件。问题是未选中的复选框不会作为表单值传递,因此如果表单名称不存在于表单 post 中,您可以将其设置为默认值 "false",或者您可以设置隐藏的默认值在复选框字段之后并使用第一个传递的值
MVC 中的模型绑定将为您执行此操作
由于脚本只是查看服务器控件,因此更加简单
if (q.QuestionType.ToLower() == "yesorno")
{
var cb = new Checkbox();
cb.Id = "cb_" + q.id;
cb.Value = "true;
// add to table cell
}
// On Postback|Save
if (ctrl is Checkbox)
{
sres.Result = (ctrl as Checkbox).Checked ? "true" : "false"
}
对于列表框,您必须将 SelectionMode 设置为 multiple 并设置要呈现的大小。在回发时,如果 SelectionMode 是多个,则您将需要循环项目并连接结果。
if (myListBox.SelectionMode == SelectionMode.Multiple)
{
var selected = new List<string>();
foreach (var item in myListBox.Items)
if (item.Selected)
selected.Add(item.Text);
response = string.Join(",", selected);
}
由于语法错误,列表框没有呈现
一般来说,工作代码在这里
//create list in run time
if (q.QuestionType == "MultiSelect")
{
lstmulti = new ListBox();
lstmulti.ID = "lst_" + q.ID;
lstmulti.Width = Unit.Percentage(41);
lstmulti.Height = Unit.Percentage(100);
lstmulti.SelectionMode = ListSelectionMode.Multiple;
//to retrive the option values
if (!string.IsNullOrEmpty(q.Options))
{
string[] values = q.Options.Split(',');
foreach (string v in values)
lstmulti.Items.Add(v.Trim());
}
tc.Controls.Add(lstmulti);
}
tc.Width = Unit.Percentage(80);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
pnlSurvey.Controls.Add(tbl);
}
保存中
else if (ctrc is ListBox)
{
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;
sres.Response = string.Join(",", selected) ;
}
}
}
我正在关注这个 post
这是一个简单的应用程序,管理员可以在其中准备问卷和简单的调查,并与在我们网站上注册的任何人共享。
最终用户完成调查后,网站管理员(或任何获得授权的人)可以分析调查结果和其他任何形式的反馈,例如图形或文本。
-- 但是里面有些东西坏了--
添加问题时选择问题类型,所以我做了这个class
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 不工作)
此应用程序使用 entity framework - 有一个部分可以添加问题。它看起来像这样:
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";
它是这样显示的
它工作正常(一个复选框)
做两个checkbox(最好做一个radiobutton) 我创造并工作得很好
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";
}
我们检查最后一个单选按钮(在第一个单选按钮之后创建)
--- 现在我只需要创建一个列表 select 从表单中选择多个选项并将其作为字符串发送到数据库
--- 当我尝试添加 alistbox 时 colud 添加一个 multible select quesion type to asurvey
这是代码片段
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
//在运行 时间内创建列表 如果 (q.QuestionType.ToLower() == "MultiSelect") { ListBox 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);
}
//在post-保存
else if (ctrc is ListBox)
{
//sres.Response = (ctrc as ListBox).SelectionMode.ToString();
sres.Response = (ctrc as ListBox).Items.ToString();
}
注意事项 :: 我正在使用 asp.net 带有实体框架的网络表单
您可以轻松地按照示例添加一个具有 "true" 值的复选框控件。问题是未选中的复选框不会作为表单值传递,因此如果表单名称不存在于表单 post 中,您可以将其设置为默认值 "false",或者您可以设置隐藏的默认值在复选框字段之后并使用第一个传递的值
MVC 中的模型绑定将为您执行此操作
由于脚本只是查看服务器控件,因此更加简单
if (q.QuestionType.ToLower() == "yesorno")
{
var cb = new Checkbox();
cb.Id = "cb_" + q.id;
cb.Value = "true;
// add to table cell
}
// On Postback|Save
if (ctrl is Checkbox)
{
sres.Result = (ctrl as Checkbox).Checked ? "true" : "false"
}
对于列表框,您必须将 SelectionMode 设置为 multiple 并设置要呈现的大小。在回发时,如果 SelectionMode 是多个,则您将需要循环项目并连接结果。
if (myListBox.SelectionMode == SelectionMode.Multiple)
{
var selected = new List<string>();
foreach (var item in myListBox.Items)
if (item.Selected)
selected.Add(item.Text);
response = string.Join(",", selected);
}
由于语法错误,列表框没有呈现 一般来说,工作代码在这里
//create list in run time
if (q.QuestionType == "MultiSelect")
{
lstmulti = new ListBox();
lstmulti.ID = "lst_" + q.ID;
lstmulti.Width = Unit.Percentage(41);
lstmulti.Height = Unit.Percentage(100);
lstmulti.SelectionMode = ListSelectionMode.Multiple;
//to retrive the option values
if (!string.IsNullOrEmpty(q.Options))
{
string[] values = q.Options.Split(',');
foreach (string v in values)
lstmulti.Items.Add(v.Trim());
}
tc.Controls.Add(lstmulti);
}
tc.Width = Unit.Percentage(80);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
pnlSurvey.Controls.Add(tbl);
}
保存中
else if (ctrc is ListBox)
{
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;
sres.Response = string.Join(",", selected) ;
}
}
}