使用C#从ASP.NET中的DetailsView重新创建记录值

时间:2013-06-03 21:12:44

标签: c# asp.net detailsview

我有以下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(); //它在这里打破了“Exception Details:System.NullReferenceException:Object reference not not set to object of instance。”

非常感谢任何帮助!

0 个答案:

没有答案