2012-04-25 116 views
1

我有一個問題,我想在gridview中選擇一行,然後點擊鼠標。在gridview中選擇一行鼠標點擊

我的代碼是這樣的:

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 

      e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvdetails, "Select$" + e.Row.RowIndex); 
     } 
    } 

它無法正常工作。我不知道爲什麼?

PLZ建議我關於這一點。

「謝謝」

回答

3

找到的教程的ASP.Net select row in gridview
在ASPX頁面下的GridView標籤附加:

<SelectedRowStyle BackColor="Orange" /> 

在後面的代碼嘗試以下操作:

protected override void Render(System.Web.UI.HtmlTextWriter writer) 
{ 
    foreach (GridViewRow row in PeopleGridView.Rows) { 
     if (row.RowType == DataControlRowType.DataRow) { 
      row.Attributes["onmouseover"] = 
       "this.style.cursor='hand';this.style.textDecoration='underline';"; 
      row.Attributes["onmouseout"] = 
       "this.style.textDecoration='none';"; 
      // Set the last parameter to True 
      // to register for event validation. 
      row.Attributes["onclick"] = 
      ClientScript.GetPostBackClientHyperlink(PeopleGridView, 
       "Select$" + row.DataItemIndex, true); 
     } 
    } 
    base.Render(writer); 
} 

然後,您可以使用RowCommand捕獲此事件(類似)。

private void PeopleGridView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Select") { 
     // Get the list of customers from the session 
     List<Customer> customerList = 
       Session["Customers"] as List<Customer>; 

     Debug.WriteLine(customerList[Convert.ToInt32(e.CommandArgument)].LastName); 
    } 
}