从detailview获取当前值

时间:2014-01-21 09:15:23

标签: c# asp.net .net

我在asp.net和Sql中开发一个测验引擎作为db。我使用sqldatsource从数据库中获取测验的所有问题,然后将sql数据源数据绑定到详细信息视图

当用户在选择答案后按“提交”按钮时,系统将保存当前问题编号和用户对数组列表的答案,并且详细视图页面索引将增加以显示下一个问题

但问题是我无法获取绑定到dataview的当前数据行如何从Detailview获取当前的问题编号。有人可以帮忙吗?

ASP.NET:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
                 BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
                 CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Vertical" 
                 Height="50px" Width="477px" ForeColor="Black">
    <AlternatingRowStyle BackColor="#CCCCCC" />
    <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <Fields>
        <asp:BoundField DataField="Question" HeaderText="Question" 
                        SortExpression="Question" />
        <asp:BoundField DataField="Answer 1" HeaderText="Answer 1" 
                        SortExpression="Answer 1" />
        <asp:BoundField DataField="Answer 2" HeaderText="Answer 2" 
                        SortExpression="Answer 2" />
        <asp:BoundField DataField="Answer 3" HeaderText="Answer 3" 
                        SortExpression="Answer 3" />
        <asp:BoundField DataField="Answer 4" HeaderText="Answer 4" 
                        SortExpression="Answer 4" />
        <asp:BoundField DataField="Correct Answer" HeaderText="Correct Answer" 
                        SortExpression="Correct Answer" />
        <asp:BoundField DataField="Id" SortExpression="Id" />
    </Fields>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:QuizEngineConnectionString %>" 
                   SelectCommand="SELECT Question_Bank.Qstn_id AS Id, Qusetion_Master.Question, Qusetion_Master.Answer1 AS [Answer 1], Qusetion_Master.Answer2 AS [Answer 2], Qusetion_Master.Answer3 AS [Answer 3], Qusetion_Master.Answer4 AS [Answer 4], Qusetion_Master.CorrectAns AS [Correct Answer] FROM Qusetion_Master INNER JOIN Question_Bank ON Qusetion_Master.QuestionID = Question_Bank.Qstn_id WHERE (Question_Bank.Quiz_id = @Param1)">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="0" Name="Param1" 
                                  QueryStringField="quizid" />
    </SelectParameters>
</asp:SqlDataSource>

<td>
    <asp:DropDownList ID="DropDownList1" runat="server" Height="23px" Width="128px">
        <asp:ListItem Value="Answer1">Answer 1</asp:ListItem>
        <asp:ListItem>Answer 2</asp:ListItem>
        <asp:ListItem Value="Answer3">Answer 3</asp:ListItem>
        <asp:ListItem>Answer 4</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                Text="Next" Width="121px" />

代码:

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {   
        //Save off previous answers
        System.Data.DataRowView dr = (System.Data.DataRowView)DetailsView1 .DataItem;
        // Create Answer object to save values
        Answer a = new Answer();
        a.QuestionID = dr[0].ToString();
        a.CorrectAnswer = dr["Correct Answer"].ToString();
        a.UserAnswer = DropDownList1 .SelectedValue.ToString();

        ArrayList al = (ArrayList)Session["AnswerList"];
        al.Add(a);

        //  Session.Add("AnswerList", al);
        //  DetailsView1.PageIndex++;
    } Catch(exception)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

这意味着如果您的正确答案是修复行号,您可以使用DetailsView1.Rows [5](它的最后2行),还是它的动态?