TemplateField.HeaderTemplate 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置用于显示 TemplateField 对象的标头部分的模板。
public:
virtual property System::Web::UI::ITemplate ^ HeaderTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer))]
public virtual System.Web.UI.ITemplate HeaderTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer))>]
member this.HeaderTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property HeaderTemplate As ITemplate
属性值
一个实现了 ITemplate 的对象,该对象包含用于显示数据绑定控件中 TemplateField 的标头部分的模板。 默认值为 null
,指示未设置此属性。
- 属性
示例
下面的代码示例演示如何使用HeaderTemplate属性为控件中GridView字段列的TemplateField标题部分创建自定义模板。 该模板显示一个复选框,允许用户显示或隐藏控件的 GridView 行。
<%@ 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">
void SelectAllCheckBox_CheckedChanged(Object sender, EventArgs e)
{
// Get the CheckBox control that indicates whether to show or
// hide the rows in the GridView control. The sender parameter
// contains the control that raised the event.
CheckBox showCheckBox = (CheckBox)sender;
// Show or hide the rows of the GridView control based
// on the check box value selected by the user.
if (showCheckBox.Checked)
{
ShowRows (true);
}
else
{
ShowRows (false);
}
}
void ShowRows(bool show)
{
// Iterate through the Rows collection of the GridView
// control and show or hide the rows based on the value
// of the show parameter.
foreach(GridViewRow row in AuthorsGridView.Rows)
{
row.Visible = show;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateField HeaderTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TemplateField HeaderTemplate Example</h3>
<!-- Populate the Columns collection declaratively. -->
<!-- Create a TemplateField column field that contains -->
<!-- a CheckBox control in the header section to show or -->
<!-- hide the rows in the GridView control. -->
<asp:gridview id="AuthorsGridView"
datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="False"
width="250"
runat="server">
<columns>
<asp:templatefield>
<headerstyle backcolor="Navy"
forecolor="White"/>
<itemtemplate>
<%#Eval("au_fname")%>
<%#Eval("au_lname")%>
</itemtemplate>
<headertemplate>
<asp:checkbox id="ShowAllCheckBox"
text="Show All"
checked="True"
autopostback="true"
oncheckedchanged="SelectAllCheckBox_CheckedChanged"
runat="server"/>
</headertemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub SelectAllCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
' Get the CheckBox control that indicates whether to show or
' hide the rows in the GridView control. The sender parameter
' contains the control that raised the event.
Dim showCheckBox As CheckBox = CType(sender, CheckBox)
' Show or hide the rows of the GridView control based
' on the check box value selected by the user.
If showCheckBox.Checked Then
ShowRows(True)
Else
ShowRows(False)
End If
End Sub
Sub ShowRows(ByVal show As Boolean)
' Iterate through the Rows collection of the GridView
' control and show or hide the rows based on the value
' of the show parameter.
Dim row As GridViewRow
For Each row In AuthorsGridView.Rows
row.Visible = show
Next
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TemplateField HeaderTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TemplateField HeaderTemplate Example</h3>
<!-- Populate the Columns collection declaratively. -->
<!-- Create a TemplateField column field that contains -->
<!-- a CheckBox control in the header section to show or -->
<!-- hide the rows in the GridView control. -->
<asp:gridview id="AuthorsGridView"
datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="False"
width="250"
runat="server">
<columns>
<asp:templatefield>
<headerstyle backcolor="Navy"
forecolor="White"/>
<itemtemplate>
<%#Eval("au_fname")%>
<%#Eval("au_lname")%>
</itemtemplate>
<headertemplate>
<asp:checkbox id="ShowAllCheckBox"
text="Show All"
checked="True"
autopostback="true"
oncheckedchanged="SelectAllCheckBox_CheckedChanged"
runat="server"/>
</headertemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
使用 HeaderTemplate 属性指定为对象的标头节 TemplateField 显示的自定义内容。 通过创建一个模板来定义内容,该模板指定标头节的呈现方式。
若要指定模板,请先在元素的<TemplateField>
开始标记和结束标记之间打开和结束<HeaderTemplate>
标记。 接下来,在开始标记和结束 <HeaderTemplate>
标记之间添加自定义内容。 内容可以像纯文本或更复杂的 (在模板中嵌入其他控件(例如) )一样简单。
若要以编程方式访问模板中定义的控件,请先确定数据绑定控件中的哪个 TableCell 对象包含该控件。 接下来,使用 Controls 对象的集合 TableCell 来访问控件。 如果控件指定ID了属性,还可以使用FindControl对象的方法TableCell查找控件。