如何添加一个复选框并将其绑定在此ListView中?

时间:2012-07-25 08:54:32

标签: c# asp.net listview checkbox

我正在开发的Quiz Management基于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,那么该怎么做?

更新

这是一个快照,向您展示我在代码中缺少的内容: enter image description here

2 个答案:

答案 0 :(得分:1)

修改SqlDatasource3的查询可以解决问题

enter image description here

        <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)"

答案 1 :(得分:1)

将sqldatasource3 select语句更改为

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