1

我是创建 ASP.Net 页面的新手。我有一个带有GridView对象的基本 asp.net 页面,并且我编写了一个RowDataBound事件来根据条件更改行颜色。我需要一些关于如何将我的函数/事件链接到实际GridView对象的帮助。函数/事件应该放在客户端还是服务器端?

附言。我正在使用 Visual Studio 2010,如果有一种方法可以使用工具栏选项将函数添加到对象中,那就太棒了。

4

1 回答 1

0

RowDataBound 事件在服务器端处理。您可以在“代码隐藏”文件中包含事件代码/逻辑,也可以在 HTML 文件中添加内联脚本。

<%@ Page Language="C#" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<script runat="server">  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    {   
       // logic here
    }  
</script>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>GridView RowDataBound Event</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Navy">GridView OnRowDataBound</h2>  
        <asp:SqlDataSource   
            ID="SqlDataSource1"  
            runat="server"  
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"  
            SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products"  
            >  
        </asp:SqlDataSource>  
        <asp:GridView   
            ID="GridView1"  
            runat="server"  
            DataSourceID="SqlDataSource1"  
            ForeColor="AliceBlue"  
            BackColor="DarkSalmon"  
            BorderColor="Salmon"  
            HeaderStyle-BackColor="Crimson"  
            AllowPaging="true"  
            AutoGenerateColumns="true"  
            DataKeyNames="ProductID"  
            OnRowDataBound="GridView1_RowDataBound"  
            >  
        </asp:GridView>          
    </div>  
    </form>  
</body>  

重要的是要注意 OnRowDataBound 属性必须与您的 even 方法同名。例如OnRowDataBound="GridView1_RowDataBound"具有与事件处理程序签名相同的名称GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

有关更多信息,请参见此处

于 2013-01-04T20:31:32.707 回答