2012-07-24 31 views
1

我是一名新的ASP.NET開發人員,我必須創建一個測驗引擎應用程序。我使用三個實時查看控制以顯示: 1.測驗信息 2.提問信息(基於所選擇的測驗在第一的ListView) 3.答案信息(基於所選擇的問題在第二的ListView)如何在第一個ListView中選擇一行後顯示(可見)第二個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將顯示所選問題的答案信息。

那麼該怎麼做?

我知道我不應該張貼太多的代碼或信息,但我必須這樣做,要清楚:

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

      <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中。

UPDATE:

我嘗試以下方法,但它並沒有和我一起工作:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ListView1.SelectedIndex = -1; 
     } 
    } 
    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ListView2.DataBind(); 
    } 

回答

1

添加第一選擇行DataKey像這樣

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

而且使一定要得到的ID在SqlDataSource的

然後在Itemcommand試試這個:

ListViewDataItem dataItem = (ListViewDataItem)e.Item; 
int ID = Convert.toInt32(ListView1.DataKeys[dataItem.DisplayIndex].Values[0]); 

他再是你的ID現在你可以使第二列表視圖可見,並且用這個ID來填補第二列表視圖

希望我幫助

+0

其實,我沒有得到如何使用ItemCommand來顯示第二個ListView。如果你能告訴我 – 2012-07-24 08:41:33

+0

請打開網頁表單並在設計視圖中右擊它然後在事件中(它看起來像一個照明螺栓),然後你會發現它的ItemCommand只需雙擊它,你會在代碼隱藏 – 2012-07-24 08:52:15

+0

有ItemCommand謝謝,但我的問題是關於如何使用該ID來讓第二個ListView被顯示。無論如何,我會盡力得到它。 – 2012-07-24 09:00:16

0

試試這個: 首先把ID在datakey在第一列表視圖 然後在item命令從datakey 獲取所選行的ID,然後您可以使第二個listview可見並使用此ID填充它,並且您可以對最後一個列表視圖執行相同操作

不要忘記將第二和最後一個列表視圖上可見的僞所以不會看到它們,除非他在第一個列表視圖

+0

感謝您的幫助,但因爲我是一個新的開發者,我沒有得到你的意思是什麼你的段落。您能否詳細說明並提供一段代碼片段(如果可能的話)? – 2012-07-24 08:12:30

相關問題