2010-08-07 97 views
3

我在更新面板中使用下面的代码来选择一行,这反过来更新另一个更新面板的表单记录中的细节。在更新面板中突出显示gridview行而不回发

protected void gvMainGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //Make the entire row clickable to select this record 
      //Uses javascript to post page back 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
      e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); 

     } 
    } 

我手动从数据库绑定的GridView和不想重新绑定网只是为了突出显示该行,但我似乎无法任意JavaScript添加到onclick事件,似乎无论是显示GetPostBackClientHyperlink或行突出显示javascript。

回答

2

我挣扎在点击添加事件既行数据绑定:

e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); 

e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')"); 

追加与该行的亮点方法后回传选择“;”似乎工作。

e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "');" + ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex)); 
1

首先,您不能将文本修饰应用于<tr> ...或<td>。你需要将它应用到里面的元素。

这里有一些调整,你可以在代码中试戴

e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';"; 



e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid.ClientId, "Select$" + e.Row.RowIndex)); 

第1对我的作品。没有什么方便测试第二。

3

How to highlight gridview when row is selected

这个你在后面OnRowCreated事件文件你的代码编写代码或者您也可以写在格OnRowDataBound情况下,本代码...

protected void ctlGridView_OnRowCreated(object sender, GridViewRowEventArgs e) 
    {  
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')"); 
     }    
    } 

,并添加此一个脚本

<script language="javascript" type="text/javascript"> 
    var gridViewCtlId = '<%=ctlGridView.ClientID%>'; 
    var gridViewCtl = null; 
    var curSelRow = null; 
    function getGridViewControl() 
    { 
     if (null == gridViewCtl) 
     { 
      gridViewCtl = document.getElementById(gridViewCtlId); 
     } 
    } 

    function onGridViewRowSelected(rowIdx) 
    { 
     var selRow = getSelectedRow(rowIdx); 
     if (curSelRow != null) 
     { 
      curSelRow.style.backgroundColor = '#ffffff'; 
     } 

     if (null != selRow) 
     { 
      curSelRow = selRow; 
      curSelRow.style.backgroundColor = '#ababab'; 
     } 
    } 

    function getSelectedRow(rowIdx) 
    { 
     getGridViewControl(); 
     if (null != gridViewCtl) 
     { 
      return gridViewCtl.rows[rowIdx]; 
     } 
     return null; 
    } 
</script> 

,它会突出显示所选行..

+1

我应该提到我有代码来做突出显示,它将这个和选择一起添加,这是问题。 此代码是对我的+1的改进,谢谢。 – 2010-08-07 15:05:21