2012-01-29 55 views
0

我正在開發一個Master-Details GridView & DetailsView。一切正常,但我希望它刪除GridView中每行的選擇按鈕,並使用戶能夠通過單擊行中的任何位置或單擊行中的任何單元格來選擇行。除此之外,當他在同一行中再次單擊時選擇該行並隱藏細節時,我希望顯示每行的詳細信息。 那麼該怎麼做?如何在沒有選擇按鈕的情況下選擇和取消選擇GridView中的行?

ASP.NET代碼:

<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px"> 
         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" /> 
         <Columns> 
          <asp:CommandField ShowSelectButton="true" /> 
          <asp:BoundField DataField="QuestionID" HeaderText="Question" /> 
          <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> 

後臺代碼:

public partial class Results : System.Web.UI.Page 
{ 
    bool bShowDetailsView; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     bShowDetailsView = false; 

     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."; 
      } 

     } 


    } 


    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue; 
     bShowDetailsView = true; 
     answerDetails.Visible = bShowDetailsView; 
    } 



} 
+0

可以使用點擊事件選擇行和更改此事件中的行的顏色等 – Hadas 2012-01-29 11:48:28

+0

你能否提供我的代碼? – 2012-01-29 11:50:00

回答

0

阿里,這是想要做一個平常的事和一個簡單的谷歌搜索會返回幾個關於如何實現它的優秀教程。

基本上,你可以將下面的代碼添加到您的RowDataBound事件:

if (e.row.RowType == DataControlRowType.DataRow) { 
     e.row.Attributes["onmouseover"] = 
      "this.style.cursor='hand';"; 
     e.row.Attributes["onmouseout"] = 
      "this.style.textDecoration='none';"; 
     // Set the last parameter to True 
     // to register for event validation. 
     e.row.Attributes["onclick"] = 
     ClientScript.GetPostBackClientHyperlink(CustomerGridView, 
      "Select$" + row.RowIndex, true); 
    } 

完整指南在這裏:http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx

+0

感謝您的幫助。我用你的代碼,我得到了以下錯誤,我不知道爲什麼:RegisterForEventValidation只能在Render(); – 2012-01-30 03:57:42

+1

最後,我明白了。我需要做的只是在aspx頁面的頁面指令中添加EnableEventValidation =「false」。 – 2012-01-30 04:21:46

相關問題