如何在第一个ListView中选择一行后显示/显示第二个ListView?

时间:2012-07-25 05:00:49

标签: c# asp.net listview

我是一名新的ASP.NET开发人员,我必须创建一个测验引擎应用程序。我使用三个LiveView控件来显示:

测验信息 问题信息(基于第一个ListView上的选定测验) ,以及< strong> 回答信息(基于第二个ListView上的选定问题)

我有以下数据库设计:

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

我现在想要的是:当用户(管理员)来到测验引擎页面时,他应该只看到第一个主要是测验信息的ListView。当他选择其中一个测验时,第二个ListView将显示所选测验的问题信息,依此类推第三个ListView。我只是在显示第二个ListView时遇到问题。我不知道如何展示它。

那怎么做?

我知道我不应该发布过多的代码或信息,但我必须这样做才能说清楚:

<div align="center">
            <asp:ListView ID="ListView1" runat="server" DataKeyNames="QuizID" 
                DataSourceID="SqlDataSource1" 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="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="DescriptionTextBox" runat="server" 
                                Text='<%# Bind("Description") %>' />
                        </td>
                    </tr>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table id="Table1" 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>
                            &nbsp;</td>--%>
                        <td>
                            <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="DescriptionTextBox" runat="server" 
                                Text='<%# Bind("Description") %>' />
                        </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" />

                            <asp:ImageButton ID="SelectButton" ImageUrl="Images/icons/select.png" ToolTip="Select"  runat="server" CommandName="Select" />
                            <%--<asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Select" />--%>
                        </td>
                        <%--<td>
                            <asp:Label ID="QuizIDLabel" runat="server" 
                                Text='<%# Eval("QuizID") %>' />
                        </td>--%>
                        <td>
                            <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DescriptionLabel" runat="server" 
                                Text='<%# Eval("Description") %>' />
                        </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; ">Title</th>
                                <th style="border-bottom:2px solid #003366; ">Description</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="QuizIDLabel" runat="server" 
                                Text='<%# Eval("QuizID") %>' />
                        </td>--%>
                        <td>
                            <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DescriptionLabel" runat="server" 
                                Text='<%# Eval("Description") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 

                SelectCommand="SELECT * FROM [Quiz]" 
                DeleteCommand="DELETE FROM [Quiz] WHERE [QuizID] = @QuizID" 
                InsertCommand="INSERT INTO [Quiz] ([Title], [Description]) VALUES (@Title, @Description)" 


                UpdateCommand="UPDATE [Quiz] SET [Title] = @Title, [Description] = @Description WHERE [QuizID] = @QuizID">
                <DeleteParameters>
                    <asp:Parameter Name="QuizID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
                    <asp:Parameter Name="QuizID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
    </div>

    <br /><br />

    <%--Second ListView that will contain the content of the quiz--%>
    <div align="center">
        <asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" 
            DataKeyNames="QuestionID" InsertItemPosition="LastItem" visible="false">

            <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:Label ID="QuestionIDLabel1" runat="server" 
                            Text='<%# Eval("QuestionID") %>' />
                    </td>--%>
                    <td>
                        <asp:TextBox ID="QuestionTextBox" runat="server" 
                            Text='<%# Bind("Question") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="QuestionOrderTextBox" runat="server" 
                            Text='<%# Bind("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerExplanationTextBox" runat="server" 
                            Text='<%# Bind("AnswerExplanation") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

            <EmptyDataTemplate>
                <table 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>
                        &nbsp;</td>--%>
                    <td>
                        <asp:TextBox ID="QuestionTextBox" runat="server" 
                            Text='<%# Bind("Question") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="QuestionOrderTextBox" runat="server" 
                            Text='<%# Bind("QuestionOrder") %>'/>
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerExplanationTextBox" runat="server" 
                            Text='<%# Bind("AnswerExplanation") %>' />
                    </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" />

                        <asp:ImageButton ID="SelectButton" ImageUrl="Images/icons/select.png" ToolTip="Select" runat="server" CommandName="Select" />
                            <%--<asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Select" />--%>
                    </td>
                    <td>
                        <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
                    </td>
                    <td>
                        <asp:Label ID="QuestionOrderLabel" runat="server" 
                            Text='<%# Eval("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AnswerExplanationLabel" runat="server" 
                            Text='<%# Eval("AnswerExplanation") %>' />
                    </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; ">Question</th>
                                <th style="border-bottom:2px solid #003366; ">Question Order</th>
                                <th style="border-bottom:2px solid #003366; ">Answer Explanation</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="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
                    </td>
                    <td>
                        <asp:Label ID="QuestionOrderLabel" runat="server" 
                            Text='<%# Eval("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AnswerExplanationLabel" runat="server" 
                            Text='<%# Eval("AnswerExplanation") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        </div>

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT [Question].* FROM [Question] INNER JOIN [QuizContent] ON [QuizContent].[QuizID] = @QuizID"

            DeleteCommand="DELETE FROM [Question] WHERE [QuestionID] = @QuestionID" 
            InsertCommand="INSERT INTO [Question] ([Question], [QuestionOrder], [AnswerExplanation]) VALUES (@Question, @QuestionOrder, @AnswerExplanation)" 


            UpdateCommand="UPDATE [Question] SET [Question] = @Question, [QuestionOrder] = @QuestionOrder, [AnswerExplanation] = @AnswerExplanation WHERE [QuestionID] = @QuestionID">

                <DeleteParameters>
                    <asp:Parameter Name="QuestionID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="QuestionOrder" Type="Int32" />
                    <asp:Parameter Name="AnswerExplanation" Type="String" />
                    <asp:ControlParameter ControlID="ListView1" Name="QuizID" PropertyName="SelectedValue" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="QuestionOrder" Type="Int32" />
                    <asp:Parameter Name="AnswerExplanation" Type="String" />
                </UpdateParameters>

            <SelectParameters>
                <asp:ControlParameter ControlID="ListView1" Name="QuizID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

        <br /><br />

    <%--Third ListView that will contain the content of the quiz--%>
    <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:Label ID="AnswerIDLabel1" runat="server" Text='<%# Eval("AnswerID") %>' />
                    </td>--%>
                    <td>
                        <asp:TextBox ID="AnswerTextBox" runat="server" 
                            Text='<%# Bind("Answer") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

            <EmptyDataTemplate>
                <table 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>
                        &nbsp;</td>--%>
                    <td>
                        <asp:TextBox ID="AnswerTextBox" runat="server" 
                            Text='<%# Bind("Answer") %>'/>
                    </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="AnswerIDLabel" runat="server" Text='<%# Eval("AnswerID") %>' />
                    </td>--%>
                    <td>
                        <asp:Label ID="AnswerLabel" runat="server" Text='<%# Eval("Answer") %>' />
                    </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>
                        </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>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        </div>

        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT [Answers].* FROM [Answers] INNER JOIN [QuizContent] ON [QuizContent].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" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

注意: 我没有任何代码隐藏。问题又如下:当我运行代码时,我将看到三个ListView控件。它应该只显示其中一个。当用户选择其中一行时,将显示第二个ListView,依此类推第三个ListView。

2 个答案:

答案 0 :(得分:1)

我记得在同一个项目中帮助你解决相关问题 - How to develop this ListView which depends on another ListView? - 你从未跟进过。查看您的历史记录,您似乎打算让Stack Overflow社区为您完成所有工作。几乎每个问题都与这个项目的不同方面有关。

rant-&gt;如果我是你,我会为这种方法感到难过,因为你 没有表现出主动权,采取所有权的动力 你的手艺。当你收获时,你似乎满足于让别人辛苦劳作 赏金,而你的理解水平几乎没有从它的推动 初始点。但无论如何......我们中的一些人只是想解决 问题,并始终努力扩大我们的技能。我们希望更多 我们的同事们对这项工艺充满热情和投入。 &lt; - 结束咆哮

溶液 - &GT; 我在其他帖子中提到的相同内容适用于此处。首先,你的设计有问题。在这里,您希望将ListView2(问题)绑定到ListView1(测验),其中模式中没有任何内容可以加入它们!你可以做一些深奥的,错综复杂的连接,通过QuizContents表的键将问题与测验联系到其他两个表,但是在你的演示文稿中,QuizContents来自这两个,所以即使这样也行不通。在您当前的设置中无法完成此平坦操作。看看我在上面提到的链接中推荐的内容,即使你不同意,如果你花点时间再考虑一下设计,然后赶紧去攻击并编写代码,你会发现你是什么尝试不起作用。改造您的设计。溶液 - &GT;

答案 1 :(得分:1)

好的,我已经帮助过你一个类似的问题了,这一次,我决定确切地看到这个问题,所以我用你的结构在本地创建了表格,我可以复制你的问题

基本上你的查询是错误的,我不知道你期望如何隐藏/显示ListView控件而不编写代码或JavaScript ......

我必须说我同意@ GrayFox374你应该表现出更多的主动性......

好吧无论如何......

我已经测试了这段代码,我认为这正是您所寻找的:

基本上,当页面加载时,只有quizes表可见,当选择测验时,如果有记录,则显示问题表,最后当你选择一个问题时,只有当答案表包含数据时才会显示答案表。

输出

加载页面时

enter image description here

选择测验时

enter image description here

选择问题时

enter image description here

背后的代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.InitializeListView(this.ListView2);
            this.InitializeListView(this.ListView3);
        }
    }

    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.InitializeListView(this.ListView2);
        this.InitializeListView(this.ListView3);
    }

    protected void ListView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.InitializeListView(this.ListView3);
    }

    private void InitializeListView(ListView listView)
    {
        listView.SelectedIndex = -1;
        listView.EditIndex = -1;
        listView.DataBind();

        listView.Visible = listView.Items.Count > 0;
    }

ASPX

注意:只显示我更新的代码

     <asp:ListView ID="ListView1" runat="server" DataKeyNames="QuizID" 
                DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" 
                onselectedindexchanged="ListView1_SelectedIndexChanged" >
.....

 <asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" 
            DataKeyNames="QuestionID" InsertItemPosition="LastItem"
            onselectedindexchanged="ListView2_SelectedIndexChanged">
....
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT        QuestionID, Question, QuestionOrder, AnswerExplanation
FROM            Question
WHERE        (QuestionID IN
                             (SELECT DISTINCT QuestionID
                               FROM            QuizContent
                               WHERE        (QuizID = @QuizID)))"
            <SelectParameters>
                <asp:ControlParameter ControlID="ListView1" Name="QuizID" DefaultValue="0" 
                PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
.....

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

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

请注意,如果这不起作用,我强烈建议您针对数据库手动运行查询以丢弃数据问题

嗯,我希望我不会忘记添加更多修改