2015-09-11 127 views
1

這是我的aspx代碼。簡單地說,它有一個gridview,我想添加一些javascipt代碼。類型'System.Web.UI.WebControls.GridView'沒有名爲'script'的公共屬性

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="MainForm.aspx.cs" 
Inherits="GUI_MainForm" EnableSessionState="True" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="Server"> 
<div style="float:left"> 
    <asp:Label runat="server" ID="label1"></asp:Label> 
</div> 
<div style="float:right"> 
    <asp:GridView ID="gridviewFriend" Width="200px" runat="server" BorderStyle="None" ShowHeader="False" OnRowDataBound ="gridviewFriendRowDataBound">    
     <script type="text/javascript" > 
      function onMouseOver(rowIndex) { 
       var gv = document.getElementById("gridviewFriend"); 
       var rowElement = gv.rows[rowIndex]; 
       rowElement.style.backgroundColor = "#c8e4b6"; 
       rowElement.cells[1].style.backgroundColor = "green"; 
      } 

      function onMouseOut(rowIndex) { 
       var gv = document.getElementById("gridviewFriend"); 
       var rowElement = gv.rows[rowIndex]; 
       rowElement.style.backgroundColor = "#fff"; 
       rowElement.cells[1].style.backgroundColor = "#fff"; 
      } 
</script>   
    </asp:GridView> 
</div> 
</asp:Content> 

我不知道爲什麼,當我插入一些JavaScript代碼,它顯示錯誤:

Type System.Web.UI.WebControls.GridView does not have a public property named script

回答

1

請刪除script標籤,並做到這一點在你的RowDataBound事件中。

protected void gridviewFriendRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#c8e4b6';"); 
     e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#fff';"); 
    } 
} 

框架是你的朋友!

+0

那麼,這工作。萬分感謝。 – anhtv13

+0

第二個單元格的備用背景顏色如何? –

+0

@DrazenBjelovuk:對不起,我錯過了。漫長的一天。明天 – naveen

1

您正在將您的Javascript放入GridView服務器標籤中。外移動它,並調整它是這樣的:

<asp:GridView ID="gridviewFriend" Width="200px" runat="server" BorderStyle="None" ShowHeader="False" OnRowDataBound ="gridviewFriendRowDataBound"> 
</asp:GridView>   
<script type="text/javascript" > 
    var gridRows = document.getElementById('gridviewFriend').rows; 

    for (var i = 0; i < gridRows.length; i++) { 
     gridRows[i].onmouseover = function() { changeColor.call(this, "#c8e4b6", "green"); }; 
     gridRows[i].onmouseout = function() { changeColor.call(this, "fff", "fff"); }; 
    } 

    function changeColor(general, firstCell) { 
     this.style.backgroundColor = general; 
     this.cells[1].style.backgroundColor = firstCell; 
    } 
</script> 

或者更好的是,折騰它一起,這CSS添加到您的樣式表:

#gridviewFriend tr { 
    background-color: "fff"; 
} 

#gridviewFriend tr:hover { 
    background-color: "#c8e4b6"; 
} 

#gridviewFriend tr:hover > td:nth-child(2) { 
    background-color: "green"; 
} 
0

你是做了錯誤的方式。嘗試按照本文介紹的方式進行操作。

http://www.aspsnippets.com/Articles/How-to-change-GridView-Row-Color-on-Mouseover-in-ASPNet.aspx

您需要的腳本標籤放置在頁面的您<head>部分,或者你的情況"HeadContent"部分,像這樣:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> 
    <script type="text/javascript"> 
     $(function() { 
      $("[id*='gridviewFriend'] td").hover(function() { 
       $("td", $(this).closest("tr")).addClass("hover_row"); 
      },function(){ 
       $("td", $(this).closest("tr")).removeClass("hover_row"); 
      }); 
     }); 
    </script> 
</asp:Content> 
+0

不應該這是一個評論? – naveen

+0

感謝您快速回復@naveen。添加了一些代碼示例:) –

+2

這假定他使用jQuery。 –

相關問題