Javascript 类型';System.Web.UI.WebControls.GridView';没有名为';脚本';

Javascript 类型';System.Web.UI.WebControls.GridView';没有名为';脚本';,javascript,asp.net,Javascript,Asp.net,这是我的aspx代码。简单地说,它有一个gridview,我想添加一些javascipt代码 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="MainForm.aspx.cs" Inherits="GUI_MainForm" EnableSessionState="True" %> <asp:Content ID="Content1

这是我的aspx代码。简单地说,它有一个gridview,我想添加一些javascipt代码

<%@ 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>

onMouseOver函数(行索引){
var gv=document.getElementById(“gridviewFriend”);
var rowElement=gv.rows[rowIndex];
rowElement.style.backgroundColor=“#c8e4b6”;
rowElement.cells[1].style.backgroundColor=“绿色”;
}
onMouseOut函数(行索引){
var gv=document.getElementById(“gridviewFriend”);
var rowElement=gv.rows[rowIndex];
rowElement.style.backgroundColor=“#fff”;
rowElement.cells[1].style.backgroundColor=“#fff”;
}
我不知道为什么当我插入一些javascript代码时,会显示错误:

Type
System.Web.UI.WebControls.GridView
没有公共 名为脚本的属性


.

您正在将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>
<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>

你做得不对。试着按照本文描述的方式来做

您需要将脚本标记放在页面的
部分,或者在您的情况下放在
的“HeadContent”
部分,如下所示:

<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>
<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>

$(函数(){
$(“[id*='gridviewFriend']td”).hover(函数(){
$(“td”,$(this).closest(“tr”).addClass(“hover_row”);
},函数(){
$(“td”和$(this).closest(“tr”).removeClass(“hover_row”);
});
});

请删除
脚本
标记,并在
行数据绑定
事件中执行此操作

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';");
    }
}

框架是你的朋友

这不是一个评论吗?谢谢你的快速回复@naveen。添加了一些代码示例:)这假设他正在使用jQuery。@NaveedButt:始终存在rowdatabound。hmmm:)为什么是jquery依赖项?第二个单元格的备用背景色如何?@DrazenBjelovuk:对不起,我错过了。漫长的一天。明天