1

開発中のクイズ管理WebベースアプリケーションにListViewがあり、データベースのAnswersテーブルにバインドしています。ここで、データベースのQuizContentテーブルの(isCorrect)列にバインドされるCheckBoxコントロールを含む列を追加します。どうすればいいのかわからないので、手伝ってくれませんか?

参考までに、私は次のデータベース設計を持っています。

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>

繰り返しますが、私の問題は、データベースに2つの異なるテーブルがあることです。ListViewはAnswerテーブルにバインドされています。また、CheckBoxの場合、QuizContentである2番目のテーブルにバインドする必要があるので、それを行うにはどうすればよいですか?

アップデート:

これが私がコードに欠けているものを示すためのスナップショットです: ここに画像の説明を入力してください

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

sqldatasource3selectステートメントを次のように変更します

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 に答える