2012-04-22 188 views

回答

0

你可以尝试调用JavaScript本功能离子在onmouseover事件。 This website有一个简单的例子:

在服务器端:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onmouseover"] = 
      "javascript:mouseovercolor(this);"; 
     e.Row.Attributes["onmouseout"] = 
      "javascript:mouseoutcolor(this);"; 
    } 
} 

在客户端:

<script language=javascript type="text/javascript"> 
    function mouseovercolor(mytxt) { 
     mytxt.bgColor = 'Orange'; 
    } 
    function mouseoutcolor(mytxt) { 
     element.bgColor = 'White'; 
    } 
</script> 

编辑:This site has a nice example如何使其与onClick事件工作:

服务器端:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e){ 
    if (e.Row.RowType == DataControlRowType.DataRow){ 
    // javascript function to call on row-click event 
    e.Row.Attributes.Add("onClick", "javascript:void SelectRow(this);"); 
    } 
} 

客户端:

<script type="text/javascript"> 
     // format current row 
     function SelectRow(row) { 
      var _selectColor = "#303030"; 
      var _normalColor = "#909090"; 
      var _selectFontSize = "3em"; 
      var _normalFontSize = "2em"; 
      // get all data rows - siblings to current 
      var _rows = row.parentNode.childNodes; 
      // deselect all data rows 
      try { 
       for (i = 0; i < _rows.length; i++) { 
        var _firstCell = _rows[i].getElementsByTagName("td")[0]; 
        _firstCell.style.color = _normalColor; 
        _firstCell.style.fontSize = _normalFontSize; 
        _firstCell.style.fontWeight = "normal"; 
       } 
      } 
      catch (e) { } 
      // select current row (formatting applied to first cell) 
      var _selectedRowFirstCell = row.getElementsByTagName("td")[0]; 
      _selectedRowFirstCell.style.color = _selectColor; 
      _selectedRowFirstCell.style.fontSize = _selectFontSize; 
      _selectedRowFirstCell.style.fontWeight = "bold"; 
     } 
</script> 
+0

谢谢,但我有这个错误,当我调试它:错误只有内容控件被允许直接在包含内容控件的内容页面。 – Gandhi 2012-04-22 15:08:56

+0

你在使用母版页吗?如果是这样,只是为了排除故障,将javascript块移到母版页(头标记) – Ulises 2012-04-22 15:14:34

+0

感谢我的朋友的帮助,但是当我单击网格视图中的另一个单元格时,我想重置前一个单元格的值并应用新细胞的新价值。 – Gandhi 2012-04-22 15:29:50

2

你可以做的是在GridView标签下的aspx页面:

<SelectedRowStyle BackColor="Orange" /> 

但是,如果你想在鼠标或鼠标不同的颜色了,然后尝试背后下RowDataBound事件中的代码下面

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.backgroundColor='orangered'"); 
      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'"); 
     } 
    } 

还检查了这个链接,如果你想选择一个行不点击的按钮:ASP.NET: Selecting a Row in a GridView

+0

谢谢Habib.OSU,我用(GridView1_RowDataBound)事件的代码,但我想,当我在GridView的项目点击,就必须突出和强调仍直到我点击另一个。 – Gandhi 2012-04-22 14:53:09

+0

@甘地,看看这个链接,http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx我也更新答案与链接 – Habib 2012-04-22 15:06:16

相关问题