2012-07-17 145 views
0

我是一名新的ASP.NET開發人員,我正在嘗試開發一個簡單的測驗引擎,它將允許系統管理員使用兩個ListView創建測驗。第一個用於插入測驗題目和說明的ListView,第二個ListView用於插入問題,答案(不同數量的答案),正確答案,答案解釋,問題順序。如何開發這個依賴於另一個ListView的ListView?

我有以下的數據庫設計:

Quiz Table: QuizID, Title, Description 
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation 
QuestionImage Table: ID, QuestionID, URL 
Answer Table: AnswerID, Answer 
QuizContent Table: ID, QuizID, QuestionID, AnswerID 

,我已經讓我感到困惑與數據綁定第二ListView中的要求:

  1. 每次測驗有不同數量的問題,並每個問題有 不同數量的可能答案。例如,在其中一個測驗中,我有兩個問題。在第一個問題中,我有四個可能的答案,第二個問題是真或假問題。
  2. 有些問題可能會有一些圖像。

此ListView應該支持CRUDE操作。 那麼該怎麼做?

我的ASP.NET代碼爲先的ListView:

<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" Width="20px" runat="server" CommandName="Update" /> 

          <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" Width="20px" 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/create 2 48.png" Width="20px" runat="server" CommandName="Insert" /> 

          <asp:ImageButton ID="CancelButton" ImageUrl="images/clear3.png" Width="20px" 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" Width="20px" runat="server" CommandName="Delete" /> 

          <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit224.png" Width="20px" runat="server" CommandName="Edit" /> 

          <asp:ImageButton ID="SelectButton" ImageUrl="images/select.png" Width="20px" 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/delete24.png" Width="20px" runat="server" CommandName="Delete" /> 

          <asp:ImageButton ID="EditButton" ImageUrl="images/edit224.png" Width="20px" 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> 

而且我對二ListView控件代碼:

<div align="center"> 
     <asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" 
      DataKeyNames="QuestionID" InsertItemPosition="LastItem"> 

      <EditItemTemplate> 

       <tr style="background-color: #FFCC66;color: #000080;"> 
        <td> 
         <asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" Width="20px" runat="server" CommandName="Update" /> 

         <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" Width="20px" 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") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="Answer1TextBox" runat="server" Text='<%# Bind("Answer") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="CorrectAnswerTextBox" runat="server" 
          Text='<%# Bind("CorrectAnswer") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="AnswerExplanationTextBox" runat="server" 
          Text='<%# Bind("AnswerExplanation") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="QuestionOrderTextBox" runat="server" 
          Text='<%# Bind("QuestionOrder") %>' CssClass="textbox" /> 
        </td> 
       </tr> 
      </EditItemTemplate> 

      <EmptyDataTemplate> 
       <table id="Table2" runat="server" 
        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;"> 
        <tr> 
         <td> 
          No data was returned.</td> 
        </tr> 
       </table> 
      </EmptyDataTemplate> 

      <InsertItemTemplate> 
       <tr style=""> 
        <td> 
         <asp:ImageButton ID="ImageButton1" ImageUrl="images/insert.png" Width="20px" runat="server" CommandName="Insert" /> 

         <asp:ImageButton ID="CancelButton" ImageUrl="images/clear3.png" Width="20px" runat="server" CommandName="Cancel" /> 
        </td> 
        <%--<td> 
         &nbsp;</td>--%> 
        <td> 
         <asp:TextBox ID="QuestionTextBox" runat="server" Text='<%# Bind("Question") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="AnswerTextBox" runat="server" Text='<%# Bind("Answer1") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <%-- to hide the bollon when mouse out, just add onmouseout="BalloonPopupControlBehavior.hidePopup(); --%> 
         <asp:TextBox ID="CorrectAnswerTextBox" runat="server" Text='<%# Bind("CorrectAnswer") %>' CssClass="textbox"/> 
        </td> 
        <td> 
         <asp:TextBox ID="AnswerExplanationTextBox" runat="server" Text='<%# Bind("AnswerExplanation") %>' CssClass="textbox" /> 
        </td> 
        <td> 
         <asp:TextBox ID="QuestionOrderTextBox" runat="server" Text='<%# Bind("QuestionOrder") %>' CssClass="textbox" /> 
        </td> 
       </tr> 

       <%-- --%> 
       <ajaxtoolkit:balloonpopupextender ID="BalloonPopupExtender1" runat="server" 
              TargetControlID="CorrectAnswerTextBox" BalloonPopupControlID="pnlBallon" 
              Position="BottomRight" BalloonStyle="Cloud" BalloonSize="Small" 
              CustomCssUrl="ballonPopupStyle" 
        CustomClassName="oval" UseShadow="true" ScrollBars="Auto" 
              DisplayOnMouseOver="false" DisplayOnFocus="true" 
        DisplayOnClick="true" > 
       </ajaxToolkit:BalloonPopupExtender> 

       <asp:Panel ID="pnlBallon" runat="server"> 
        Please enter the letter of the correct answer A, B, C, D. 
       </asp:Panel> 

      </InsertItemTemplate> 

      <ItemTemplate> 
       <tr style="background-color: #FFFBD6;color: #333333;"> 
        <td> 
         <asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" Width="20px" runat="server" CommandName="Delete" /> 

          <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit224.png" Width="20px" runat="server" CommandName="Edit" /> 
        </td> 
        <td> 
         <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="Answer1Label" runat="server" Text='<%# Eval("Answer") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="CorrectAnswerLabel" runat="server" 
          Text='<%# Eval("CorrectAnswer") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="AnswerExplanationLabel" runat="server" 
          Text='<%# Eval("AnswerExplanation") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="QuestionOrderLabel" runat="server" 
          Text='<%# Eval("QuestionOrder") %>' /> 
        </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; ">Answer</th> 
           <th style="border-bottom:2px solid #003366; ">Correct Answer</th> 
           <th style="border-bottom:2px solid #003366; ">Answer Explanation</th> 
           <th style="border-bottom:2px solid #003366; ">Question Order</th> 
           <th style="border-bottom:2px solid #003366; ">Image</th> 
          </tr> 
         </thead> 
         <tbody><tr id="itemPlaceholder" runat="server"></tr></tbody> 
       </table></div> 

      </LayoutTemplate> 
      <SelectedItemTemplate> 
       <tr style="background-color: #FFCC66;font-weight: bold;color: #000080;"> 
        <td> 
         <asp:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" /> 
         <%--<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
          Text="Delete" />--%> 
         <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="Edit" /> 
         <%--<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />--%> 
        </td> 
        <td> 
         <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="Answer1Label" runat="server" Text='<%# Eval("Answer") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="CorrectAnswerLabel" runat="server" 
          Text='<%# Eval("CorrectAnswer") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="AnswerExplanationLabel" runat="server" 
          Text='<%# Eval("AnswerExplanation") %>' /> 
        </td> 
        <td> 
         <asp:Label ID="QuestionOrderLabel" runat="server" 
          Text='<%# Eval("QuestionOrder") %>' /> 
        </td> 
       </tr> 
      </SelectedItemTemplate> 
     </asp:ListView> 
     </div> 
     </ContentTemplate> 
     </asp:UpdatePanel> 

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
      ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
      SelectCommand="SELECT  dbo.Question.Question, dbo.Question.QuestionOrder, dbo.Question.AnswerExplanation, dbo.Answers.Answer, dbo.QuestionImage.URL 
          FROM   dbo.Question INNER JOIN 
                dbo.QuizContent ON dbo.Question.QuestionID = dbo.QuizContent.QuestionID INNER JOIN 
                dbo.Answers ON dbo.QuizContent.AnswerID = dbo.Answers.AnswerID INNER JOIN 
                dbo.Quiz ON dbo.QuizContent.QuizID = dbo.Quiz.QuizID LEFT OUTER JOIN 
                dbo.QuestionImage ON dbo.Question.QuestionID = dbo.QuestionImage.QuestionID 
          WHERE  (dbo.QuizContent.QuizID = @QuizID)"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="ListView1" Name="QuizID" 
        PropertyName="SelectedValue" Type="Int32" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
    </div> 

我現在的問題是我怎麼可以讓管理員能夠按照他的意願輸入不同數量的答案。 任何人都可以幫助我嗎?

+0

這根本不是很清楚。您需要澄清要求。我甚至不會冒險解決問題,因爲如果你有一點點關閉,人們會在短時間內讓你失望。這聽起來像你想要使用第一個listview的選定值在第二個的where子句中使用。這似乎很簡單。我在正確的軌道上嗎? – GrayFox374 2012-07-17 05:33:16

+0

感謝您的反饋。你能看看我更新的問題嗎? – 2012-07-17 06:32:35

+0

當然。我要睡覺,並在早上看看它。 – GrayFox374 2012-07-17 06:39:07

回答

0

我一直在想這個和原型,我想你會更好地通過修改設計服務。與其試圖在兩個listView中做一頁一頁的所有內容,可能最好模擬一個工作流並將其在多頁面上傳播。在第一頁使用你的第一個listView,它在Quiz(zes)表上實現你想要的CRUD操作。代碼這樣,當選擇特定測驗,你推動工作提高到一個新的頁面,或者通過將QuizID作爲參數的URL - 示例:?

的Response.Redirect(「Questions.aspx quiz_id = 1024" );

或爲一個會話變量 - 實例:

會話[ 「quiz_id」] = 1024;的Response.Redirect( 「Questions.aspx」);

此時,由於您不在問題或答案表中使用QuizID,所以在工作流程稍後您不需要此選項,但它稍後會派上用場。使用與測驗相同的方式對listView進行編碼,然後進入答案並重復。

在管理員給出問題和答案之後,將他們引導至一個集成頁面(或多個頁面),現在您可以最終使用您已攜帶的QuizID。或者你可以提供一個小的列表框來選擇一個測驗,每個listBoxItem的文本是標題和/或描述,值是QuizID。

無論哪種方式,在這一點上,您可以添加listBoxes的問題和解答什麼是可用的,以及他們旁邊的列表框選擇什麼。添加左右箭頭按鈕可以爲兩組方框來回移動選項。

此時,在頁面和click事件上添加一個保存按鈕,在QuizContents表上執行插入操作,使用QuizID變量以及來自所選問答列表框的QuestionID和AnswerID。如果您在這些列表框中的任何一箇中允許多選模式,您將必須循環執行此操作。

最後,有一個確認頁面,他們可以通過QuizID進行搜索,並一目瞭然地查看所有相關的問題和答案。