2012-04-22 187 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

相關問題