我有以下 DetailsView 设置:
<asp:DetailsView ID="QuestionDetails" runat="server" AutoGenerateRows="False" DataKeyNames="QuestionID,QuizID" DataSourceID="SqlDataSource_Quiz" OnPageIndexChanged="QuestionDetails_PageIndexChanged">
<Fields>
<asp:TemplateField HeaderText="Text_Eng" SortExpression="Text_Eng" ShowHeader="False">
<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_Eng") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option2" AccessKey="2" runat="server" Text='<%# Bind("Opt_2_Eng") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option3" AccessKey="3" runat="server" Text='<%# Bind("Opt_3_Eng") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
基本上,我正在显示一个问题以及来自我已经验证工作的 sqlDataSource 的三个选项。
我需要做的是使用按钮循环浏览记录(问题和选项)。然后在我后面的代码中,我想检索一些列的值。
这是我背后的代码:
protected void nextButton_Click(object sender, EventArgs e)
{
//try
//{
// Save off previous answers
System.Data.DataRowView dr = (System.Data.DataRowView)questionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionNo"].ToString(); //it breaks here saying "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object."
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
//}
//catch (Exception ex)
//{
// Response.Redirect("default.aspx");
//}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Enabled = false;
//go to evaluate answer page
//Response.Redirect("About.aspx");
}
else
{
QuestionDetails.PageIndex += 1;
NextButton.Enabled = false;
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Text = "Finish the Quiz";
}
}
protected void Option_CheckedChanged(object sender, EventArgs e)
{
if (NextButton.Enabled == false)
{
NextButton.Enabled = true;
}
selectedOptionRB = (RadioButton)sender;
}
这是我遇到的问题: a.QuestionID = dr["QuestionNo"].ToString(); //它在这里中断说“异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。”
任何帮助将不胜感激!