2012-01-10 41 views
0

我正在嘗試開發一個爲用戶提供簡短測驗的Web應用程序。我正在嘗試關注並利用ASP.NET網站中關於Quiz Engine的解釋示例。我在結果頁面中有一個Master-Details,當用戶在GridView中選擇他的一個應答問題時,該問題的詳細信息將顯示在GridView下面的DetailsView中。一切正常。Master-Detail GridView和DetailsView?

我現在想要的只是使DetailsView(這是回答問題的詳細信息)隱藏,除非用戶選擇一個回答的問題。那麼該怎麼做?

此外,如果我將使用Ajax而不是PostBack來顯示此DetailsView,那將會非常好。

我的ASP.NET代碼:

<tr> 
       <td> 
        <asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="True" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px"> 
         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" /> 
         <Columns> 
          <asp:BoundField DataField="QuestionID" HeaderText="Question" /> 
          <%--<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" />--%> 
          <asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" /> 
          <asp:BoundField DataField="Result" HeaderText="Result" /> 
         </Columns> 
         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" /> 
         <EditRowStyle BackColor="#999999" /> 
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
        </asp:GridView> 

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
         SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]"> 
         <SelectParameters> 
          <asp:SessionParameter Name="QuizID" SessionField="QuizID" Type="Int32" /> 
         </SelectParameters> 
        </asp:SqlDataSource>     
       </td> 
      </tr> 
      <tr> 
       <td> 
        <asp:DetailsView ID="answerDetails" runat="server" CellPadding="4" ForeColor="#333333" 
         GridLines="None" Height="45px" Width="552px" DataSourceID="SqlDataSource1" 
         AutoGenerateRows="False" DataKeyNames="QuestionID"> 

         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" /> 
         <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="100px" /> 
         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <EditRowStyle BackColor="#999999" /> 
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
         <Fields> 
          <asp:BoundField DataField="Question" HeaderText="Question" 
           SortExpression="Question" /> 
          <asp:BoundField DataField="Answer1" HeaderText="A" 
           SortExpression="Answer1" /> 
          <asp:BoundField DataField="Answer2" HeaderText="B" 
           SortExpression="Answer2" /> 
          <asp:BoundField DataField="Answer3" HeaderText="C" 
           SortExpression="Answer3" /> 
          <asp:BoundField DataField="Answer4" HeaderText="D" 
           SortExpression="Answer4" /> 
          <asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" 
           SortExpression="CorrectAnswer" HeaderStyle-BackColor="lightgreen" /> 
          <asp:BoundField DataField="AnswerExplanation" HeaderText="Explanation" 
           SortExpression="AnswerExplanation" HeaderStyle-BackColor="lightgreen" /> 
         </Fields> 
        </asp:DetailsView>     
       </td> 
      </tr> 

編輯:

enter image description here

代碼隱藏我已經是:

protected void Page_Load(object sender, EventArgs e) 
    { 
     ArrayList al = (ArrayList)Session["AnswerList"]; 

     if (al == null) 
     { 
      Response.Redirect("default.aspx"); 
     } 

     resultGrid.DataSource = al; 
     resultGrid.DataBind(); 

     // Save the results into the database. 
     if (IsPostBack == false) 
     { 
      // Calculate score 
      double questions = al.Count; 
      double correct = 0.0; 


      for (int i = 0; i < al.Count; i++) 
      { 
       Answer a = (Answer)al[i]; 
       if (a.Result == Answer.ResultValue.Correct) 
        correct++; 
      } 

      double score = (correct/questions) * 100; 
      string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", ""); 
      SqlDataSource userQuizDataSource = new SqlDataSource(); 
      userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString(); 
      userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)"; 

      userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString()); 
      userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString()); 

      // "N4" is for displaying four decimal places, regardless of what the value is 
      userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4")); 

      userQuizDataSource.InsertParameters.Add("Username", username); 

      int rowsAffected = userQuizDataSource.Insert(); 
      if (rowsAffected == 0) 
      { 
       // Let's just notify that the insertion didn't 
       // work, but let' s continue on ... 
       errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu."; 


      } 

     } 


    } 

回答

0

我想你需要更新SqlDataSource1以獲取其參數er的值來自GridView,因此除非用戶在GridView中選擇一行,否則detailsView不會出現。嘗試這個。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=" 
    <%$ConnectionStrings:testConnectionString %>" 
    SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], 
    [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM 
    [Question] WHERE ([QuestionID] = @QuestionID) ORDER BY [QuestionOrder]"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="resultGrid" Name="QuestionID" 
         PropertyName="SelectedValue" Type="Int32" /> 
        </SelectParameters> 
</asp:SqlDataSource> 

我希望這將解決您的問題

+0

感謝您的幫助。我嘗試了你給我的東西,但它確實和我一起工作。 DetailsView消失了,但是當我點擊除了一個回答的問題之外的選擇時,什麼也沒有顯示。我想要的是當我點擊選擇時顯示DetailsView。怎麼做? – user1093651 2012-01-10 12:38:57

+0

這就是我在選擇它應該顯示的gridView行時所建議的內容。您是否看到了我在「WHERE([QuestionID] = @QuestionID)」上面編輯選擇命令的方式「按照原樣複製代碼並嘗試。它應該在resulGrid – Mubarek 2012-01-10 13:07:26

+0

中的DataKeyNames =「QuestionID」上工作順便檢查您正在使用的選擇按鈕。如果可能,發佈它 – Mubarek 2012-01-10 13:16:50

相關問題