[英]How to link my Gridview function to the GridView object
我是创建ASP.Net页面的新手。 我有一个带有GridView
对象的基本asp.net页,并且编写了一个RowDataBound
事件以根据条件更改行颜色。 我需要一些有关如何将函数/事件链接到实际GridView
对象的帮助。 函数/事件应该放在客户端还是服务器端?
ps。 我正在使用Visual Studio 2010,如果有一种方法可以使用很棒的工具栏选项向对象添加功能。
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)
具有相同的名称。
有关更多信息,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.