我有一个代码,根据问题类型生成一个新问题,并在每次用户点击下一个按钮时用新问题更新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++;
}
}
答案 0 :(得分:0)
单击按钮时,不要创建收音机或复选框。在VS页面设计器或preinit中创建它们,否则它将不会存储在viewstate中,您将无法获得它。在dsigner或preinit中创建控件后,根据您的问题类型显示/隐藏/设置值。这样您就可以获得先前控件的值(基本上是当前控件)。否则你可以使用传统的requst [“id”]方式获取数据,但这将是混乱的。
答案 1 :(得分:0)
尝试使用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>
然后从按钮上的代码隐藏点击事件切换活动视图到您想要的那个。通过这种方式,您可以获取任何控件的值,无论它是否处于活动视图中。