2013-12-09 42 views
0

我正在使用下面的GetPostBackClientHyperlink來填充行選擇相關信息的文本框。我也希望選中的行在回發後突出顯示。第二個屬性將突出顯示該行,但不會回發。我似乎無法讓他們一起工作。回發後在gridview中突出顯示選定的行

任何想法?

aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); 
     //e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex); 
    } 

回答

3

你必須從行回傳後添加亮點的onclick,像這樣:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
    BindData();//Your method to set datasource anddatabind GridView1 
    GridView1.Rows[index].Attributes.Add("style","background-color:yellow"); 
    // Even better add a class here so that you have more control from css 
    // GridView1.Rows[index].Attributes.Add("class", "mycustomclass"); 
} 

而且你可以從GridView1_RowDataBound()移動註釋行:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); 
} 

編輯:

這裏是我的標記,WebForm3.aspx

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="GridViewTest.WebForm3" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
      OnRowDataBound="GridView1_RowDataBound" 
      OnRowCommand="GridView1_RowCommand"> 
     </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 

和代碼,WebForm3.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Web.UI.WebControls; 

namespace GridViewTest 
{ 
    public partial class WebForm3 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       BindData(); 
      } 

     } 
     private void BindData() 
     { 
      var lstItems = new List<ListItem>() 
      { 
       new ListItem {Text ="Items 1", Value ="1"}, 
       new ListItem {Text ="Items 2", Value ="2"}, 
       new ListItem {Text ="Items 3", Value ="3"}, 
       new ListItem {Text ="Items 4", Value ="4"} 

      }; 
      GridView1.DataSource = lstItems; 
      GridView1.DataBind(); 
     } 


     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); 
      //e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex); 

     } 

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      BindData(); 
      GridView1.Rows[index].Attributes.Add("style", "background-color:yellow"); 
      GridView1.Rows[index].Attributes.Add("class", "mycustomclass"); 
     } 
    } 

} 

而且點擊後一排,這是我強調的GridView :

enter image description here

如果仍然無法正常工作,可以下載測試項目以與代碼進行比較。測試項目是here

+0

謝謝@afzalulh的迴應,不幸的是我仍然無法讓它工作。它看起來像GridView_RowCommand事件在回發之前觸發。我設置消息框彈出以查看調試事件的順序。它看起來像GridView_RowCommand觸發,改變顏色。但是,然後頁面重新加載並丟失格式。 – ChipHappens

+0

我希望你不要重組網格。在我的代碼中,屬性在'BindData()'和'BindData'應該有'GridView1.DataBind();'後設置。我可以發佈我使用過的測試項目。 – afzalulh

+0

@ChipHappens - 請參閱編輯。 – afzalulh