我已经开始创建在线测验了。我正在使用链接到sqlDataSource的DetailsView。 这是我的代码:
<asp:DetailsView ID="QuestionDetails" runat="server" AutoGenerateRows="False" DataKeyNames="QuestionID,QuizID" DataSourceID="SqlDataSource_Quiz">
<Fields>
<asp:TemplateField HeaderText="Text_Eng" SortExpression="Text_Eng" ShowHeader="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option1" AccessKey="1" runat="server" Text='<%# Bind("Opt_1") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged" />
<br />
<asp:RadioButton ID="Option2" AccessKey="2" runat="server" Text='<%# Bind("Opt_2") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
<asp:RadioButton ID="Option3" AccessKey="3" runat="server" Text='<%# Bind("Opt_3") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
请注意,我没有在DetailsView中显示从sqlDataSource检索的所有数据(还有其他字段/列)。
现在,在我的代码后面,我通过单击按钮(NextButton)更改DetailsView的PageIndex来显示下一条记录,但同时,我需要检索当前显示记录的一些数据,所以我'将以下内容放在按钮的单击处理程序中:
protected void NextButton_Click(object sender, EventArgs e)
{
try
{
// Save off previous answers
DataRowView dr = (DataRowView)QuestionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionID"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = selectedOptionRB.AccessKey.ToString();
ArrayList al = (ArrayList)Session["userAnswers"];
al.Add(a);
Session.Add("userAnswers", al);
}
catch (Exception ex)
{
Label2.Text = "Exception!";
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Enabled = false;
}
else
{
QuestionDetails.PageIndex += 1;
NextButton.Enabled = false;
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Text = "Finish the Quiz";
}
}
因此,当我运行代码时,我得到“异常!”显示在我设置用于测试的Label2中。
我做错了什么?