GridView 类说明

GridView 类

注意:此类在 .NET Framework 2.0 版中是新增的。

在表中显示数据源的值,其中每列表示一个字段,每行表示一条记录。GridView 控件允许您选择和编辑这些项以及对它们进行排序。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl08e8963be,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl08img,"; </script> 语法语法
Visual Basic(声明)
<ControlValuePropertyAttribute("SelectedValue")> _
Public Class GridView
    Inherits CompositeDataBoundControl
    Implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler
Visual Basic(用法)
Dim instance As GridView
C#
[ControlValuePropertyAttribute("SelectedValue")] 
public class GridView : CompositeDataBoundControl, IPostBackContainer, IPostBackEventHandler, ICallbackContainer, 
    ICallbackEventHandler
C++
[ControlValuePropertyAttribute(L"SelectedValue")] 
public ref class GridView : public CompositeDataBoundControl, IPostBackContainer, IPostBackEventHandler, ICallbackContainer, 
    ICallbackEventHandler
J#
/** @attribute ControlValuePropertyAttribute("SelectedValue") */ 
public class GridView extends CompositeDataBoundControl implements IPostBackContainer, IPostBackEventHandler, 
    ICallbackContainer, ICallbackEventHandler
JScript
ControlValuePropertyAttribute("SelectedValue") 
public class GridView extends CompositeDataBoundControl implements IPostBackContainer, IPostBackEventHandler, 
    ICallbackContainer, ICallbackEventHandler
备注备注
列字段

GridView 控件中的每一列由一个

DataControlField 对象表示。默认情况下,AutoGenerateColumns 属性被设置为 true,为数据源中的每一个字段创建一个 AutoGeneratedField 对象。每个字段然后作为 GridView 控件中的列呈现,其顺序同于每一字段在数据源中出现的顺序。

通过将 AutoGenerateColumns 属性设置为 false,然后定义您自己的列字段集合,您也可以手动控制哪些列字段将显示在 GridView 控件中。不同的列字段类型决定控件中各列的行为。下表列出了可以使用的不同列字段类型。

列字段类型

说明

BoundField

显示数据源中某个字段的值。这是 GridView 控件的默认列类型。

ButtonField

GridView 控件中的每个项显示一个命令按钮。这使您可以创建一列自定义按钮控件,如“添加”按钮或“移除”按钮。

CheckBoxField

GridView 控件中的每一项显示一个复选框。此列字段类型通常用于显示具有布尔值的字段。

CommandField

显示用来执行选择、编辑或删除操作的预定义命令按钮。

HyperLinkField

将数据源中某个字段的值显示为超链接。此列字段类型允许您将另一个字段绑定到超链接的 URL。

ImageField

GridView 控件中的每一项显示一个图像。

TemplateField

根据指定的模板为 GridView 控件中的每一项显示用户定义的内容。此列字段类型允许您创建自定义的列字段。

若要以声明方式定义列字段集合,请首先在 GridView 控件的开始和结束标记之间添加 <Columns> 开始和结束标记。接着,列出您想包含在 <Columns> 开始和结束标记之间的列字段。指定的列将以所列出的顺序添加到 Columns 集合中。Columns 集合存储该控件中的所有列字段,并允许您以编程方式管理 GridView 控件中的列字段。

显式声明的列字段可与自动生成的列字段结合在一起显示。两者同时使用时,先呈现显式声明的列字段,再呈现自动生成的列字段。

Note注意

自动生成的列字段不会添加到 Columns 集合中。

绑定到数据

GridView 控件可绑定到数据源控件(如 SqlDataSourceObjectDataSource 等等),以及实现 System.Collections.IEnumerable 接口的任何数据源(如 System.Data.DataViewSystem.Collections.ArrayListSystem.Collections.Hashtable)。使用以下方法之一将 GridView 控件绑定到适当的数据源类型:

  • 若要绑定到某个数据源控件,请将 GridView 控件的 DataSourceID 属性设置为该数据源控件的 ID 值。GridView 控件自动绑定到指定的数据源控件,并且可利用该数据源控件的功能来执行排序、更新、删除和分页功能。这是绑定到数据的首选方法。

  • 若要绑定到某个实现 System.Collections.IEnumerable 接口的数据源,请以编程方式将 GridView 控件的 DataSource 属性设置为该数据源,然后调用 DataBind 方法。当使用此方法时,GridView 控件不提供内置的排序、更新、删除和分页功能。需要使用适当的事件提供此功能。

有关数据绑定的更多信息,请参见 通过 ASP.NET 访问数据

Note注意

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,请检查它们是否包含可执行脚本、SQL 语句或其他代码。只要有可能,强烈建议在这些值显示在此控件中之前对它们进行 HTML 编码(默认情况下,BoundField 类会对值进行 HTML 编码)。ASP.NET 提供输入请求验证功能以阻止用户输入中的脚本和 HTML。还提供验证服务器控件以判断用户输入。有关更多信息,请参见 验证控件介绍

数据操作

GridView 控件提供了很多内置功能,这些功能使得用户可以对控件中的项进行排序、更新、删除、选择和分页。当 GridView 控件绑定到某个数据源控件时,GridView 控件可利用该数据源控件的功能并提供自动排序、更新和删除功能。

Note注意

GridView 控件可为其他类型的数据源提供对排序、更新和删除的支持;但是,您必须提供一个适当的事件处理程序,其中包含对这些操作的实现。

排序允许用户通过单击某个特定列的标题来根据该列排序 GridView 控件中的项。若要启用排序,请将 AllowSorting 属性设置为 true

当单击 ButtonFieldTemplateField 列字段中命令名分别为“Edit”、“Delete”和“Select”的按钮时,自动更新、删除和选择功能启用。如果 AutoGenerateEditButtonAutoGenerateDeleteButtonAutoGenerateSelectButton 属性分别设置为 true 时,GridView 控件可自动添加带有“编辑”、“删除”或“选择”按钮的 CommandField 列字段。

Note注意

GridView 控件不直接支持将记录插入数据源。但是,通过将 GridView 控件与 DetailsViewFormView 控件结合使用则可以插入记录。有关更多信息,请分别参见 DetailsViewFormView

GridView 控件可自动将数据源中的所有记录分成多页,而不是同时显示这些记录。若要启用分页,请将 AllowPaging 属性设置为 true

自定义用户界面

您可以通过设置 GridView 控件的不同部分的样式属性自定义该控件的外观。下表列出了不同的样式属性。

样式属性

说明

AlternatingRowStyle

GridView 控件中的交替数据行的样式设置。当设置了此属性时,数据行交替使用 RowStyle 设置和 AlternatingRowStyle 设置进行显示。

EditRowStyle

GridView 控件中正在编辑的行的样式设置。

EmptyDataRowStyle

当数据源不包含任何记录时,GridView 控件中显示的空数据行的样式设置。

FooterStyle

GridView 控件的脚注行的样式设置。

HeaderStyle

GridView 控件的标题行的样式设置。

PagerStyle

GridView 控件的页导航行的样式设置。

RowStyle

GridView 控件中的数据行的样式设置。当还设置了 AlternatingRowStyle 属性时,数据行交替使用 RowStyle 设置和 AlternatingRowStyle 设置进行显示。

SelectedRowStyle

GridView 控件中的选中行的样式设置。

也可以显示或隐藏控件的不同部分。下表列出控制显示或隐藏哪些部分的属性。

属性

说明

ShowFooter

显示或隐藏 GridView 控件的页脚节。

ShowHeader

显示或隐藏 GridView 控件的页眉节。

事件

GridView 控件提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了 GridView 控件支持的事件。

事件

说明

PageIndexChanged

在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。此事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行某项任务。

PageIndexChanging

在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。此事件通常用于取消分页操作。

RowCancelingEdit

在单击某一行的“取消”按钮时,但在 GridView 控件退出编辑模式之前发生。此事件通常用于停止取消操作。

RowCommand

当单击 GridView 控件中的按钮时发生。此事件通常用于在控件中单击按钮时执行某项任务。

RowCreated

当在 GridView 控件中创建新行时发生。此事件通常用于在创建行时修改行的内容。

RowDataBound

GridView 控件中将数据行绑定到数据时发生。此事件通常用于在行绑定到数据时修改行的内容。

RowDeleted

在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之后发生。此事件通常用于检查删除操作的结果。

RowDeleting

在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之前发生。此事件通常用于取消删除操作。

RowEditing

发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。

RowUpdated

发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。此事件通常用于检查更新操作的结果。

RowUpdating

发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。此事件通常用于取消更新操作。

SelectedIndexChanged

发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。此事件通常用于在该控件中选定某行之后执行某项任务。

SelectedIndexChanging

发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。此事件通常用于取消选择操作。

Sorted

在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之后发生。此事件通常用于在用户单击用于列排序的超链接之后执行某个任务。

Sorting

在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之前发生。此事件通常用于取消排序操作或执行自定义的排序例程。

辅助功能

默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

TopicLocation
如何:以声明方式创建 ASP.NET Web 控件模板生成 ASP .NET Web 应用程序
如何:使用 AccessDataSource 控件连接到 Access 数据库 (Visual Studio)在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:使用设计器创建 Web 服务器控件模板在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:创建 ASP.NET Web 服务器控件模板生成 ASP .NET Web 应用程序
如何:动态创建 ASP.NET Web 服务器控件模板生成 ASP .NET Web 应用程序
如何:动态设置 GridView Web 服务器控件列宽生成 ASP .NET Web 应用程序
如何:响应 DataList、Repeater 或 GridView 项中的按钮事件生成 ASP .NET Web 应用程序
如何:在 GridView Web 服务器控件中启用默认分页在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在 GridView Web 服务器控件中启用默认删除功能在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在 GridView Web 服务器控件中启用默认编辑在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在 GridView Web 服务器控件中启用默认选定内容在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在模板化控件中绑定到数据生成 ASP .NET Web 应用程序
如何:绑定到 Visual Studio 的模板化控件中的数据在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:自定义控件以便在 GridView Web 服务器控件中进行编辑在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:GridView Web 服务器控件的简单排序在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用 DetailsView 服务器控件在网页上编辑和插入数据在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用转换在 Web 窗体页中显示 XML 文档在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:具有 Visual Studio 数据组件的数据绑定网页在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建嵌套的 GridView 控件在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建网页以显示 Access 数据库数据在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 GridView Web 服务器控件中使用存储过程显示数据在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 GridView Web 服务器控件中编辑时显示下拉列表在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Studio 中创建主/详细信息网页在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:对绑定到 GridView Web 服务器控件的行执行批量更新在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:网页中的基本数据访问在 Visual Studio 中构建 ASP .NET Web 应用程序
SqlDataSource
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl1579b342f,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl15img,"; </script>
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl1026019fa7,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl102img,"; </script> 示例示例

下面的代码示例演示如何使用 GridView 控件显示 Pubs 示例数据库(在 Microsoft SQL Server 中)的 Authors 表中的值。这些值是使用 SqlDataSource 控件检索的。

Visual Basic
<%@ Page language="VB" %>

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

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

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

下面的代码示例演示如何使用 GridView 控件编辑记录。

Visual Basic
<%@ Page language="VB" %>

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

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

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

复制代码
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl107805f72d,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl107img,"; </script> .NET Framework 安全性.NET Framework 安全性
  •  
  • AspNetHostingPermission  用于在宿主环境中进行操作。要求值:
AspNetHostingPermission  用于在宿主环境中进行操作。要求值: LinkDemand;权限值: Minimal
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl11234446b1,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl112img,"; </script> 继承层次结构继承层次结构
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl120a6241fa,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl120img,"; </script>
<script type="text/Javascript"> var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl123e30d3fb,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl123img,"; </script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值