| 名称 | 说明 | |
|---|---|---|
| AccessKey | 获取或设置使您得以快速导航到 Web 服务器控件的访问键。(从 WebControl 继承。) | |
| AllowPaging | 获取或设置一个值,该值指示是否启用分页功能。 | |
| AllowSorting | 获取或设置一个值,该值指示是否启用排序功能。 | |
| AlternatingRowStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的交替数据行的外观。 | |
| AppRelativeTemplateSourceDirectory | 获取或设置包含该控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。(从 Control 继承。) | |
| Attributes | 获取与控件的属性不对应的任意特性(只用于呈现)的集合。(从 WebControl 继承。) | |
| AutoGenerateColumns | 获取或设置一个值,该值指示是否为数据源中的每个字段自动创建绑定字段。 | |
| AutoGenerateDeleteButton | 获取或设置一个值,该值指示每个数据行都带有“删除”按钮的 CommandField 字段列是否自动添加到 GridView 控件。 | |
| AutoGenerateEditButton | 获取或设置一个值,该值指示每个数据行都带有“编辑”按钮的 CommandField 字段列是否自动添加到 GridView 控件。 | |
| AutoGenerateSelectButton | 获取或设置一个值,该值指示每个数据行都带有“选择”按钮的 CommandField 字段列是否自动添加到 GridView 控件。 | |
| BackColor | 获取或设置 Web 服务器控件的背景色。(从 WebControl 继承。) | |
| BackImageUrl | 获取或设置要在 GridView 控件的背景中显示的图像的 URL。 | |
| BindingContainer | 获取包含该控件的数据绑定的控件。(从 Control 继承。) | |
| BorderColor | 获取或设置 Web 控件的边框颜色。(从 WebControl 继承。) | |
| BorderStyle | 获取或设置 Web 服务器控件的边框样式。(从 WebControl 继承。) | |
| BorderWidth | 获取或设置 Web 服务器控件的边框宽度。(从 WebControl 继承。) | |
| BottomPagerRow | 获取一个 GridViewRow 对象,该对象表示 GridView 控件中的底部页导航行。 | |
| Caption | 获取或设置要在 GridView 控件的 HTML 标题元素中呈现的文本。提供此属性的目的是使辅助技术设备的用户更易于访问控件。 | |
| CaptionAlign | 获取或设置 GridView 控件中的 HTML 标题元素的水平或垂直位置。提供此属性的目的是使辅助技术设备的用户更易于访问控件。 | |
| CellPadding | 获取或设置单元格的内容和单元格的边框之间的空间量。 | |
| CellSpacing | 获取或设置单元格间的空间量。 | |
| ClientID | 获取由 ASP.NET 生成的服务器控件标识符。(从 Control 继承。) | |
| Columns | 获取表示 GridView 控件中列字段的 DataControlField 对象的集合。 | |
| Controls | 获取复合数据绑定控件内的子控件的集合。(从 CompositeDataBoundControl 继承。) | |
| ControlStyle | 获取 Web 服务器控件的样式。此属性主要由控件开发人员使用。(从 WebControl 继承。) | |
| ControlStyleCreated | 获取一个值,该值指示是否已为 ControlStyle 属性创建了 Style 对象。此属性主要由控件开发人员使用。(从 WebControl 继承。) | |
| CssClass | 获取或设置由 Web 服务器控件在客户端呈现的级联样式表 (CSS) 类。(从 WebControl 继承。) | |
| DataKeyNames | 获取或设置一个数组,该数组包含了显示在 GridView 控件中的项的主键字段的名称。 | |
| DataKeys | 获取一个 DataKey 对象集合,这些对象表示 GridView 控件中的每一行的数据键值。 | |
| DataMember | 当数据源包含多个不同的数据项列表时,获取或设置数据绑定控件绑定到的数据列表的名称。(从 DataBoundControl 继承。) | |
| DataSource | 获取或设置对象,数据绑定控件从该对象中检索其数据项列表。(从 BaseDataBoundControl 继承。) | |
| DataSourceID | 获取或设置控件的 ID,数据绑定控件从该控件中检索其数据项列表。(从 DataBoundControl 继承。) | |
| EditIndex | 获取或设置要编辑的行的索引。 | |
| EditRowStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中为进行编辑而选中的行的外观。 | |
| EmptyDataRowStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置当 GridView 控件绑定到不包含任何记录的数据源时会呈现的空数据行的外观。 | |
| EmptyDataTemplate | 获取或设置在 GridView 控件绑定到不包含任何记录的数据源时所呈现的空数据行的用户定义内容。 | |
| EmptyDataText | 获取或设置在 GridView 控件绑定到不包含任何记录的数据源时所呈现的空数据行中显示的文本。 | |
| Enabled | 获取或设置一个值,该值指示是否启用 Web 服务器控件。(从 WebControl 继承。) | |
| EnableSortingAndPagingCallbacks | 获取或设置一个值,该值指示客户端回调是否用于排序和分页操作。 | |
| EnableTheming | 获取或设置一个值,该值指示是否对此控件应用主题。(从 WebControl 继承。) | |
| EnableViewState | 获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。(从 Control 继承。) | |
| Font | 获取与 Web 服务器控件关联的字体属性。(从 WebControl 继承。) | |
| FooterRow | 获取表示 GridView 控件中的脚注行的 GridViewRow 对象。 | |
| FooterStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的脚注行的外观。 | |
| ForeColor | 获取或设置 Web 服务器控件的前景色(通常是文本颜色)。(从 WebControl 继承。) | |
| GridLines | 获取或设置 GridView 控件的网格线样式。 | |
| HasAttributes | 获取一个值,该值指示控件是否具有属性集。(从 WebControl 继承。) | |
| HeaderRow | 获取表示 GridView 控件中的标题行的 GridViewRow 对象。 | |
| HeaderStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的标题行的外观。 | |
| Height | 获取或设置 Web 服务器控件的高度。(从 WebControl 继承。) | |
| HorizontalAlign | 获取或设置 GridView 控件在页面上的水平对齐方式。 | |
| ID | 获取或设置分配给服务器控件的编程标识符。(从 Control 继承。) | |
| NamingContainer | 获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 Control.ID 属性值的服务器控件。(从 Control 继承。) | |
| Page | 获取对包含服务器控件的 Page 实例的引用。(从 Control 继承。) | |
| PageCount | 获取在 GridView 控件中显示数据源记录所需的页数。 | |
| PageIndex | 获取或设置当前显示页的索引。 | |
| PagerSettings | 获取对 PagerSettings 对象的引用,使用该对象可以设置 GridView 控件中的页导航按钮的属性。 | |
| PagerStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的页导航行的外观。 | |
| PagerTemplate | 获取或设置 GridView 控件中页导航行的自定义内容。 | |
| PageSize | 获取或设置 GridView 控件在每页上所显示的记录的数目。 | |
| Parent | 获取对页 UI 层次结构中服务器控件的父控件的引用。(从 Control 继承。) | |
| RowHeaderColumn | 获取或设置用作 GridView 控件的列标题的列的名称。提供此属性的目的是使辅助技术设备的用户更易于访问控件。 | |
| Rows | 获取表示 GridView 控件中数据行的 GridViewRow 对象的集合。 | |
| RowStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的数据行的外观。 | |
| SelectedDataKey | 获取 DataKey 对象,该对象包含 GridView 控件中选中行的数据键值。 | |
| SelectedIndex | 获取或设置 GridView 控件中的选中行的索引。 | |
| SelectedRow | 获取对 GridViewRow 对象的引用,该对象表示控件中的选中行。 | |
| SelectedRowStyle | 获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的选中行的外观。 | |
| SelectedValue | 获取 GridView 控件中选中行的数据键值。 | |
| ShowFooter | 获取或设置一个值,该值指示是否在 GridView 控件中显示脚注行。 | |
| ShowHeader | 获取或设置一个值,该值指示是否在 GridView 控件中显示标题行。 | |
| Site | 获取容器信息,该容器在呈现于设计图面上时承载当前控件。(从 Control 继承。) | |
| SkinID | 获取或设置要应用于控件的外观。(从 WebControl 继承。) | |
| SortDirection | 获取正在排序的列的排序方向。 | |
| SortExpression | 获取与正在排序的列关联的排序表达式。 | |
| Style | 获取将在 Web 服务器控件的外部标记上呈现为样式属性的文本属性的集合。(从 WebControl 继承。) | |
| TabIndex | 获取或设置 Web 服务器控件的选项卡索引。(从 WebControl 继承。) | |
| TemplateControl | 获取或设置对包含该控件的模板的引用。 (从 Control 继承。) | |
| TemplateSourceDirectory | 获取包含当前服务器控件的 Page 或 UserControl 的虚拟目录。(从 Control 继承。) | |
| ToolTip | 获取或设置当鼠标指针悬停在 Web 服务器控件上时显示的文本。(从 WebControl 继承。) | |
| TopPagerRow | 获取一个 GridViewRow 对象,该对象表示 GridView 控件中的顶部页导航行。 | |
| UniqueID | 获取服务器控件的唯一的、以分层形式限定的标识符。(从 Control 继承。) | |
| UseAccessibleHeader | 获取或设置一个值,该值指示 GridView 控件是否以易于访问的格式呈现其标题。提供此属性的目的是使辅助技术设备的用户更易于访问控件。 | |
| Visible | 获取或设置一个值,该值指示服务器控件是否作为 UI 呈现在页上。(从 Control 继承。) | |
| Width | 获取或设置 Web 服务器控件的宽度。(从 WebControl 继承。) |
| 名称 | 说明 | |
|---|---|---|
| Adapter | 获取控件的浏览器特定适配器。(从 Control 继承。) | |
| ChildControlsCreated | 获取一个值,该值指示是否已创建服务器控件的子控件。(从 Control 继承。) | |
| ClientIDSeparator | 获取一个字符值,该值表示 ClientID 属性中使用的分隔符字符。(从 Control 继承。) | |
| Context | 为当前 Web 请求获取与服务器控件关联的 HttpContext 对象。(从 Control 继承。) | |
| DesignMode | 获取一个值,该值指示是否正在使用设计图面上的一个控件。(从 Control 继承。) | |
| Events | 获取控件的事件处理程序委托列表。此属性为只读。(从 Control 继承。) | |
| HasChildViewState | 获取一个值,该值指示当前服务器控件的子控件是否具有任何已保存的视图状态设置。(从 Control 继承。) | |
| IdSeparator | 获取用于分隔控件标识符的字符。(从 Control 继承。) | |
| Initialized | 获取一个值,该值指示数据绑定控件是否已经初始化。(从 BaseDataBoundControl 继承。) | |
| IsBoundUsingDataSourceID | 获取指示是否设置了 DataSourceID 属性的值。(从 BaseDataBoundControl 继承。) | |
| IsChildControlStateCleared | 获取一个值,该值指示该控件中包含的控件是否具有控件状态。(从 Control 继承。) | |
| IsEnabled | 获取一个值,该值指示是否启用控件。(从 WebControl 继承。) | |
| IsTrackingViewState | 获取一个值,用于指示服务器控件是否会将更改保存到其视图状态中。(从 Control 继承。) | |
| IsViewStateEnabled | 获取一个值,该值指示是否为该控件启用了视图状态。(从 Control 继承。) | |
| LoadViewStateByID | 获取一个值,该值指示控件是否通过 ID 而不是索引参与加载其视图状态。 (从 Control 继承。) | |
| RequiresDataBinding | 获取或设置一个值,该值指示是否应调用 DataBind 方法。 (从 BaseDataBoundControl 继承。) | |
| SelectArguments | 获取数据绑定控件从数据源控件检索数据时使用的 DataSourceSelectArguments 对象。 (从 DataBoundControl 继承。) | |
| TagKey | 获取 GridView 控件的 HtmlTextWriterTag 值。 | |
| TagName | 获取控件标记的名称。此属性主要由控件开发人员使用。(从 WebControl 继承。) | |
| ViewState | 获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。(从 Control 继承。) | |
| ViewStateIgnoresCase | 获取一个值,该值指示 StateBag 对象是否不区分大小写。(从 Control 继承。) |
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
</head>
<body>
<h3>AccessKey Property of a Web Control</h3>
<form runat="server">
<asp:TextBox id="TextBox1"
AccessKey="Y"
Text="Press Alt-Y to get focus here"
Columns="45"
runat="server"/>
<p>
<asp:TextBox id="TextBox2"
AccessKey="Z"
Text="Press Alt-Z to get focus here"
Columns="45"
runat="server"/>
</form>
</body>
</html>
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取或设置一个值,该值指示是否启用分页功能。
public virtual bool AllowPaging { get; set; }
属性值
如果启用分页功能,则为 true;否则为 false。默认为 false。
备注
GridView 控件可自动将数据源中的所有记录分成多页,而不是同时显示这些记录。如果数据源支持分页功能,GridView 控件可利用此功能,并提供内置的分页功能。分页功能可用于支持 System.Collections.ICollection 接口的任何数据源对象或支持分页功能的数据源。
若要启用分页功能,请将 AllowPaging 属性设置为 true。默认情况下,GridView 控件在一个页面上一次显示 10 条记录。通过设置 PageSize 属性,可更改每页上所显示记录的数目。若要确定显示数据源内容所需的总页数,请使用 PageCount 属性。通过使用 PageIndex 属性,可确定当前显示的页面的索引。
启用分页时,会在 GridView 控件中自动显示一个称为页导航行的附加行。页导航行包含允许用户导航至其他页面的控件。使用 PagerSettings 属性可以控制页导航行的设置(如页导航显示模式、同时显示的页链接数和页导航控件的文本标签)。通过设置 Position 属性,页导航行可在控件的顶部、底部或同时在顶部和底部显示。通过设置 Mode 属性,您还可以从四种内置页导航显示模式中选择一种。下表描述了内置的显示模式。
模式
说明
PagerButton.NextPrevious
一组由“上一页”和“下一页”按钮组成的分页控件。
PagerButton.NextPreviousFirstLast
一组由“上一页”、“下一页”、“首页”和“尾页”按钮组成的分页控件。
PagerButton.Numeric
一组由用于直接访问页的带编号的链接按钮组成的分页控件。这是默认模式。
PagerButton.NumericFirstLast
一组由带编号的链接按钮以及“首页”和“尾页”链接按钮组成的分页控件。
若要控制页导航行的外观(包括其背景色、字体颜色和位置),请使用 PagerStyle 属性。
注意
当数据源包含的记录少于两个时,GridView 控件会自动隐藏页导航行。
使用 GridView 控件还可以定义页导航行的自定义模板。有关创建自定义页导航行模板的更多信息,请参见 PagerTemplate。
GridView 控件提供多个可以用于在发生分页时执行自定义操作的事件。下表列出了可用的事件。
事件
说明
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。此事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行某项任务。
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。此事件通常用于取消分页操作。
Topic
Location
如何:在 GridView Web 服务器控件中启用默认分页
在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在 GridView Web 服务器控件中启用默认分页
在 Visual Studio 中生成 ASP .NET Web 应用程序
示例
下面的代码示例演示如何使用 AllowPaging 属性以声明方式在 GridView 控件中启用分页功能。
<%@ Page language="C#" %>
<html>
<body>
<form runat="server">
<h3>GridView AllowPaging Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="true"
runat="server">
<pagersettings mode="Numeric"
position="Bottom"
pagebuttoncount="10"/>
<pagerstyle backcolor="LightBlue"
height="30px"
verticalalign="Bottom"
horizontalalign="Center"/>
</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>
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取或设置一个值,该值指示是否启用排序功能。
public virtual bool AllowSorting { get; set; }
属性值
如果启用排序功能,则为 true;否则为 false。默认为 false。
当支持排序的数据源控件绑定到 GridView 控件时,GridView 控件可利用该数据源控件的功能并提供自动排序功能。
|
|
|---|
|
不同的数据源对于启用其排序功能有着不同的要求。若要确定这些要求,请参见特定数据源的文档。 |
若要启用排序,请将 AllowSorting 属性设置为 true。当启用排序时,设置了 SortExpression 属性的每个列字段的标题文本都显示为链接按钮。
|
|
|---|
|
会自动填充自动生成的列字段的 SortExpression 属性。如果通过 Columns 集合定义自己的列,则必须为每个列设置 SortExpression 属性;否则,该列不会在标题中显示链接按钮。 |
单击列的链接按钮将使 GridView 控件中的项根据排序表达式排序。通常,排序表达式仅仅是显示在列中的字段的名称,它将使 GridView 控件按照该列排序。若要按多个字段排序,请使用一个包含以逗号分隔的字段名列表的排序表达式。使用 SortExpression 属性可确定 GridView 控件正在应用的排序表达式。反复单击列的链接按钮将在升序和降序之间切换排序方向。若要确定当前排序方向,请使用 SortDirection 属性。
GridView 控件提供了多种可用于在发生排序时执行自定义操作的事件。下表列出了可用的事件。
|
事件 |
说明 |
|---|---|
|
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之后发生。此事件通常用于在用户单击用于列排序的超链接之后执行某个任务。 |
|
|
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之前发生。此事件通常用于取消排序操作或执行自定义的排序例程。 |
示例
下面的代码示例演示当使用自动生成的列时如何使用 AllowSorting 属性在 GridView 控件中启用排序。
<%@ Page language="C#" %>
<html>
<body>
<form runat="server">
<h3>GridView AllowSorting Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowsorting="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>
下面的代码示例演示在定义了 Columns 集合时如何使用 AllowSorting 属性在 GridView 控件中启用排序。还会以编程方式向正在排序的列的标题中添加一个图像以指示排序方向。若要此示例工作,您必须提供自己的图像。
<%@ Page language="C#" %>
<script runat="server">
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
// Use the RowType property to determine whether the
// row being created is the header row.
if (e.Row.RowType == DataControlRowType.Header)
{
// Call the GetSortColumnIndex helper method to determine
// the index of the column being sorted.
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
{
// Call the AddSortImage helper method to add
// a sort direction image to the appropriate
// column header.
AddSortImage(sortColumnIndex, e.Row);
}
}
}
// This is a helper method used to determine the index of the
// column being sorted. If no column is being sorted, -1 is returned.
int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
foreach (DataControlField field in CustomersGridView.Columns)
{
if (field.SortExpression == CustomersGridView.SortExpression)
{
return CustomersGridView.Columns.IndexOf(field);
}
}
return -1;
}
// This is a helper method used to add a sort direction
// image to the header of the column being sorted.
void AddSortImage(int columnIndex, GridViewRow headerRow)
{
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (CustomersGridView.SortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/Images/Ascending.jpg";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/Images/Descending.jpg";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView AllowSorting Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="false"
emptydatatext="No data available."
allowsorting="true"
onrowcreated="CustomersGridView_RowCreated"
runat="server">
<columns>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"
headerstyle-wrap="false"
sortexpression="CustomerID"/>
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
<asp:boundfield datafield="Address"
headertext="Address"
headerstyle-wrap="false"
sortexpression="Address"/>
<asp:boundfield datafield="City"
headertext="City"
headerstyle-wrap="false"
sortexpression="City"/>
<asp:boundfield datafield="PostalCode"
headertext="Postal Code"
headerstyle-wrap="false"
sortexpression="PostalCode" />
<asp:boundfield datafield="Country"
headertext="Country"
headerstyle-wrap="false"
sortexpression="Country"/>
</columns>
</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>
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取对 TableItemStyle 对象的引用,使用该对象可以设置 GridView 控件中的交替数据行的外观。
public TableItemStyle AlternatingRowStyle { get; }
属性值
一个对 TableItemStyle 的引用,该对象表示 GridView 控件中交替数据行的样式。使用 AlternatingRowStyle 属性控制 GridView 控件中交替数据行的外观。当设置了此属性时,数据行交替使用 RowStyle 设置和 AlternatingRowStyle 设置进行显示。此属性是只读的;但您可以设置它返回的 TableItemStyle 对象的属性。可以使用下列方法之一以声明方式设置这些属性:
-
在窗体 Property-Subproperty 中的 GridView 控件的开始标记中放置一个属性 (Attribute),其中 Subproperty 是 TableItemStyle 对象的属性 (Property),例如 AlternatingRowStyle-ForeColor。
-
在 GridView 控件的开始标记和结束标记之间嵌套一个 <AlternatingRowStyle> 元素。
这些属性也可以采用 Property.Subproperty 形式以编程方式设置(例如,AlternatingRowStyle.ForeColor)。常用设置通常包括自定义背景色、前景色和字体属性。
<%@ Page language="C#" %>
<html>
<body>
<form runat="server">
<h3>GridView RowStyle and AlternatingRowStyle Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
runat="server">
<rowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-italic="true"/>
<alternatingrowstyle backcolor="PaleTurquoise"
forecolor="DarkBlue"
font-italic="true"/>
</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>
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取或设置包含该控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。
public string AppRelativeTemplateSourceDirectory { get; set; }
属性值
包含该控件的页或用户控件的应用程序相对虚拟目录。
AppRelativeTemplateSourceDirectory 属性包含页或用户控件(它们包含当前控件)的应用程序相对路径。例如,如果网页位于 http://www.contoso.com/application/subdirectory,则 AppRelativeTemplateSourceDirectory 属性返回“~/subdirectory”。
若要返回虚拟路径(“application/subdirectory”),请使用 TemplateSourceDirectory 属性。
获取与控件的属性不对应的任意特性(只用于呈现)的集合。
属性值
名称和值对的 AttributeCollection。
Attributes 集合包含在 Web 服务器控件的开始标记中声明的所有属性的集合。这使您得以以编程方式控制与 Web 服务器控件关联的属性。您可以将属性添加到此集合或从此集合中移除属性。
|
|
|---|
|
此属性用控件开始标记中集合中的所有属性来呈现,与浏览器设置无关。并非所有的浏览器都支持呈现的每个属性。不受支持的属性通常被浏览器忽略。 |
|
|
|---|
|
不能使用 Attributes 集合向 WebControl 实例添加客户端脚本。要添加客户端脚本,请使用 Page 控件的 ClientScript 属性。 |
下面的示例阐释当 TextBox 控件失去焦点时,可以如何使用 WebControl 的 Attributes 属性运行 Javascript 命令。
|
|
|---|
|
以下代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型。 |
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
TextBox1.Attributes["onblur"]="javascript:alert(\'Hello! Focus lost from text box!!\');";
}
</script>
</head>
<body>
<h3>Attributes Property of a Web Control</h3>
<form runat="server">
<asp:TextBox id="TextBox1" columns=54
Text="Click here and then tap out of this text box"
runat="server"/>
</form>
</body>
</html>