键入' System.Web.UI.WebControls.GridView'没有名为' script'的公共财产

时间:2015-09-11 04:57:26

标签: 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" 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代码时,它显示错误:

  

类型System.Web.UI.WebControls.GridView没有公开   名为script

的属性

2 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分: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';");
    }
}

框架是你的朋友!