1

我正在开发的基于 Web 的测验管理应用程序中有一个 ListView,我将它绑定到数据库中的 Answers 表。现在,我想添加一个带有 CheckBox 控件的列,该控件将绑定到数据库中 QuizContent 表中的 (isCorrect) 列。我不知道该怎么做,所以你能帮我吗?

仅供参考,我有以下数据库设计:

QuizContent Table: ID, QuizID, QuestionID, AnswerID, isCorrect
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
Answers Table: AnswerID, Answer

我的 ASP.NET 代码:

<div align="center">
        <asp:ListView ID="ListView3" runat="server" DataSourceID="SqlDataSource3" 
            DataKeyNames="AnswerID" InsertItemPosition="LastItem">

            <EditItemTemplate>

                <tr style="">
                    <td>
                        <asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" ToolTip="Update" runat="server" CommandName="Update" />

                        <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerTextBox" runat="server" 
                            Text='<%# Bind("Answer") %>' />
                    </td>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"
                            Text='<%# Bind("isCorrect") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

            <EmptyDataTemplate>
                <table id="Table2" runat="server" 
                    style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>

            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="InsertButton" ImageUrl="Images/icons/add24.png" ToolTip="Add" runat="server" CommandName="Insert" />

                        <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/clear24.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerTextBox" runat="server" 
                            Text='<%# Bind("Answer") %>'/>
                    </td>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" 
                            Text='<%# Bind("isCorrect") %>' />
                    </td>
                </tr>

            </InsertItemTemplate>

            <ItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="Delete" runat="server" CommandName="Delete" />

                        <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="AnswerLabel" runat="server" Text='<%# Eval("Answer") %>' />
                    </td>
                    <td>
                        <asp:Label ID="IsCorrectLabel" runat="server" Text='<%# Eval("isCorrect") %>' />
                    </td>
                </tr>
            </ItemTemplate>

            <LayoutTemplate>
                <div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
                    <thead>
                        <tr style="background-color:#C6D7B5;">
                            <th style="border-bottom:2px solid #003366; ">...</th>
                            <th style="border-bottom:2px solid #003366; ">Answer</th>
                            <th style="border-bottom:2px solid #003366; ">is Correct?</th>
                        </tr>
                    </thead>
                    <tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
                </table></div>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="Delete" runat="server" CommandName="Delete" />

                        <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="AnswerLabel" runat="server" 
                            Text='<%# Eval("Answer") %>' />
                    </td>
                    <td>
                        <asp:Label ID="isCorrectLabel" runat="server" 
                            Text='<%# Eval("isCorrect") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        </div>

        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT        AnswerID, Answer
                            FROM            Answers
                            WHERE        (AnswerID IN
                                                         (SELECT DISTINCT AnswerID
                                                           FROM            QuizContent
                                                           WHERE        (QuestionID = @QuestionID)))"

            DeleteCommand="DELETE FROM [Answers] WHERE [AnswerID] = @AnswerID" 
            InsertCommand="INSERT INTO [Answers] ([Answer]) VALUES (@Answer)" 


            UpdateCommand="UPDATE [Answers] SET [Answer] = @Answer WHERE [AnswerID] = @AnswerID">
                <DeleteParameters>
                    <asp:Parameter Name="AnswerID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Answer" Type="String" />
                    <asp:ControlParameter ControlID="ListView2" Name="QuestionID" PropertyName="SelectedValue" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Answer" Type="String" />
                </UpdateParameters>

            <SelectParameters>
                <asp:ControlParameter ControlID="ListView2" Name="QuestionID" DefaultValue="0"
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

同样,我的问题是数据库中有两个不同的表。ListView 绑定到 Answer 表。而对于 CheckBox,它必须绑定到 QuizContent 的第二个表,那么该怎么做呢?

更新:

这是一个快照,向您展示我在代码中缺少的内容: 在此处输入图像描述

4

2 回答 2

1

修改查询就SqlDatasource3可以了

在此处输入图像描述

        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT        Answers.*, QuizContent.IsCorrect
FROM            Answers INNER JOIN
                         QuizContent ON Answers.AnswerID = QuizContent.AnswerID
WHERE        (QuizContent.QuestionID = @QuestionID)"
于 2012-07-25T09:33:47.200 回答
1

将您的 sqldatasource3 选择语句更改为

SELECT        AnswerID, Answer
                        FROM            Answers
                        WHERE        (AnswerID IN
                                                     (SELECT DISTINCT AnswerID
                                                       FROM            QuizContent
                                                       WHERE        (QuestionID = @QuestionID)))

UNION ALL 
SELECT AnswerID,isCorrect from QuizContent 
WHERE Answers.AnswerID = QuizContent.AnswerID
于 2012-07-25T09:37:27.133 回答