更新面板问题
本文关键字:问题 更新 | 更新日期: 2023-09-27 18:04:15
我有一个基于问题类型生成新问题的代码,并在每次用户单击下一个按钮时更新带有新问题的asp.net更新面板。我的问题是如何获得问题用户当前的响应,当我单击下一步时。更具体地说,我如何从更新面板读取以前的值作为下一步点击和一个新的面板加载。
public static int incremental = 0;
protected void Page_Load(object sender, EventArgs e)
{
UpdateButton2.Text = "Start the Pre-Assessment";
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
COPDEntities1 copdContext = new COPDEntities1();
UpdateButton2.Text = "NEXT";
var questions = (from p in copdContext.Questions select p).ToList();
if (incremental == questions.Count)
{
incremental = 0;
}
DateTimeLabel2.Text = questions[incremental].QuestionText;
if (questions[incremental].QuestionAnswerTypeID == 1)
{
RadioButtonList choices = new RadioButtonList();
choices.ID = "choices";
choices.ID = Convert.ToString(1);
choices.Items.Add(new ListItem("True", "0"));
choices.Items.Add(new ListItem("False", "1"));
pnlChoice.Controls.Add(choices);
}
if (questions[incremental].QuestionAnswerTypeID == 2)
{
TextBox text = new TextBox();
text.ID = "choices";
pnlChoice.Controls.Add(text);
}
if (questions[incremental].QuestionAnswerTypeID == 3)
{
int questID = questions[incremental].QuestionID;
var responses = (from r in copdContext.QuestionResponses where r.QuestionID == questID select r).ToList();
RadioButtonList choices = new RadioButtonList();
choices.ID = "choices";
choices.ID = Convert.ToString(3);
foreach(var choice in responses)
{
int choiceID= choice.ResponseID;
var multchoices = (from c in copdContext.Responses where c.ResponseID == choiceID select c).FirstOrDefault();
choices.Items.Add(new ListItem(multchoices.ResponseLongdescription, Convert.ToString(multchoices.ResponseID)));
}
pnlChoice.Controls.Add(choices);
// Label1.Text = Convert.ToString(responses[0].ResponseID);
}
if (questions[incremental].QuestionAnswerTypeID == 4)
{
int questID = questions[incremental].QuestionID;
var responses = (from r in copdContext.QuestionResponses where r.QuestionID == questID select r).ToList();
CheckBoxList choices = new CheckBoxList();
choices.ID = "choices";
choices.ID = Convert.ToString(3);
foreach (var choice in responses)
{
int choiceID = choice.ResponseID;
var multchoices = (from c in copdContext.Responses where c.ResponseID == choiceID select c).FirstOrDefault();
choices.Items.Add(new ListItem(multchoices.ResponseLongdescription, Convert.ToString(multchoices.ResponseID)));
}
pnlChoice.Controls.Add(choices);
}
if (questions[incremental].QuestionAnswerTypeID == 5)
{
TextBox text = new TextBox();
text.ID = "choices";
pnlChoice.Controls.Add(text);
}
if (questions[incremental].QuestionAnswerTypeID == 6)
{
}
if (questions[incremental].QuestionAnswerTypeID == 7)
{
RadioButtonList choices = new RadioButtonList();
}
incremental++;
}
}
单击按钮时不要创建单选或复选框。在VS页面设计器或preinit中创建它们,否则它不会存储在viewstate中,并且你无法获得它。在designer或preinit中创建控件后,根据您的问题类型显示/隐藏/设置它们的值。通过这种方式,您可以获得先前控件(基本上是当前控件)的值。否则,您可以使用传统的request ["id"]方式获取数据,但这会很混乱。
不要动态创建控件并将其添加到面板中,而是尝试使用MultiView并定义所有可能的视图:
<asp:MultiView id="MyMultiView" runat="server">
<asp:View id="FirstView" runat="server">
<!-- Markup for the first view -->
</asp:View>
<asp:View id="SecondView" runat="server">
<!-- Markup for the second view -->
</asp:View>
<asp:View id="ThirdView" runat="server">
<!-- Markup for the third view -->
</asp:View>
</asp:MultiView>
然后从代码后面的按钮上单击事件切换到您想要的活动视图。这样你就可以获取任何控件的值,无论它是否在活动视图中