The content of values that correspond to a particular field can be specified using the field’s PivotGridField.ValueTemplate property. This property takes precedence over the FieldValueTemplate property.
Note
Once a template defined via the FieldValueTemplate property is created within a control, it is instantiated within a container object of the PivotGridFieldValueTemplateContainer type. This container object exposes a set of specific properties to which the template’s child controls can be bound.
This example demonstrates how to customize the Field Header and Field Value templates, and keep automatic appearance and functionality. It is not possible to simply replace the default Header or Value element with a custom label, because these elements have a really complex layout. Ordinarily, it consists of a table including a few cells with nested items, and attached styles and scripts. These elements are generated dynamically based on the current pivot grid layout.
This example demonstrates how to customize the templates at runtime. Using this approach, it is possible to call the PivotGridFieldValueTemplateContainer.CreateFieldValue and PivotGridHeaderTemplateContainer.CreateHeader methods to generate the default template content. Then it is possible to replace some of default items with custom ones to introduce a required functionality.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingDevExpress.Web.ASPxPivotGrid;
namespaceE1805 {
publicpartialclass _Default :System.Web.UI.Page {
protectedvoidPage_Load(object sender, EventArgs e) {
ASPxPivotGrid1.HeaderTemplate = new HeaderTemplate();
ASPxPivotGrid1.FieldValueTemplate = new FieldValueTemplate();
}
}
publicclassHeaderTemplate :ITemplate {
publicvoidInstantiateIn(Control container) {
PivotGridHeaderTemplateContainer c = (PivotGridHeaderTemplateContainer)container;
PivotGridHeaderHtmlTable table = c.CreateHeader();
table.Content = new HeaderLink();
c.Controls.Add(table);
}
}
classFieldValueTemplate :ITemplate {
publicvoidInstantiateIn(Control container) {
PivotGridFieldValueTemplateContainer c = (PivotGridFieldValueTemplateContainer)container;
PivotGridFieldValueHtmlCell cell = c.CreateFieldValue();
cell.Controls.AddAt(cell.Controls.IndexOf(cell.TextControl), new HeaderLink());
cell.Controls.Remove(cell.TextControl);
c.Controls.Add(cell);
}
}
publicclassHeaderLink :HyperLink {
publicHeaderLink()
: base() {
Text = "Templated Text";
NavigateUrl = "#";
Attributes["onclick"] = "alert('this is the test prompt')";
}
}
}
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="E1805._Default" %><%@Registerassembly="DevExpress.Web.ASPxPivotGrid.v9.2, Version=9.2.10.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"namespace="DevExpress.Web.ASPxPivotGrid"tagprefix="dxwpg" %><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml" ><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><dxwpg:ASPxPivotGridID="ASPxPivotGrid1"runat="server"DataSourceID="AccessDataSource1"><Fields><dxwpg:PivotGridFieldID="fieldSalesPerson"Area="RowArea"AreaIndex="0"FieldName="Sales_Person"></dxwpg:PivotGridField><dxwpg:PivotGridFieldID="fieldExtendedPrice"Area="DataArea"AreaIndex="0"FieldName="Extended_Price"></dxwpg:PivotGridField><dxwpg:PivotGridFieldID="fieldCategoryName"Area="ColumnArea"AreaIndex="0"FieldName="CategoryName"></dxwpg:PivotGridField></Fields></dxwpg:ASPxPivotGrid><asp:AccessDataSourceID="AccessDataSource1"runat="server"DataFile="~/App_Data/nwind.mdb"SelectCommand="SELECT [Sales Person] AS Sales_Person, [Extended Price] AS Extended_Price, [CategoryName] FROM [SalesPerson]"></asp:AccessDataSource></div></form></body></html>
ImportsMicrosoft.VisualBasicImportsSystemImportsSystem.Collections.GenericImportsSystem.WebImportsSystem.Web.UIImportsSystem.Web.UI.WebControlsImportsDevExpress.Web.ASPxPivotGridNamespace E1805
PartialPublicClass _Default
Inherits System.Web.UI.Page
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As EventArgs)
ASPxPivotGrid1.HeaderTemplate = New HeaderTemplate()
ASPxPivotGrid1.FieldValueTemplate = New FieldValueTemplate()
EndSubEndClassPublicClass HeaderTemplate
Implements ITemplate
PublicSub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim c As PivotGridHeaderTemplateContainer = CType(container, PivotGridHeaderTemplateContainer)
Dim table As PivotGridHeaderHtmlTable = c.CreateHeader()
table.Content = New HeaderLink()
c.Controls.Add(table)
EndSubEndClassFriendClass FieldValueTemplate
Implements ITemplate
PublicSub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim c As PivotGridFieldValueTemplateContainer = CType(container, PivotGridFieldValueTemplateContainer)
Dim cell As PivotGridFieldValueHtmlCell = c.CreateFieldValue()
cell.Controls.AddAt(cell.Controls.IndexOf(cell.TextControl), New HeaderLink())
cell.Controls.Remove(cell.TextControl)
c.Controls.Add(cell)
EndSubEndClassPublicClass HeaderLink
Inherits HyperLink
PublicSubNew()
MyBase.New()
Text = "Templated Text"
NavigateUrl = "#"
Attributes("onclick") = "alert('this is the test prompt')"EndSubEndClassEndNamespace
<%@PageLanguage="vb"AutoEventWireup="true"CodeBehind="Default.aspx.vb"Inherits="E1805._Default" %><%@Registerassembly="DevExpress.Web.ASPxPivotGrid.v9.2, Version=9.2.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"namespace="DevExpress.Web.ASPxPivotGrid"tagprefix="dxwpg" %><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml" ><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><dxwpg:ASPxPivotGridID="ASPxPivotGrid1"runat="server"DataSourceID="AccessDataSource1"><Fields><dxwpg:PivotGridFieldID="fieldSalesPerson"Area="RowArea"AreaIndex="0"FieldName="Sales_Person"></dxwpg:PivotGridField><dxwpg:PivotGridFieldID="fieldExtendedPrice"Area="DataArea"AreaIndex="0"FieldName="Extended_Price"></dxwpg:PivotGridField><dxwpg:PivotGridFieldID="fieldCategoryName"Area="ColumnArea"AreaIndex="0"FieldName="CategoryName"></dxwpg:PivotGridField></Fields></dxwpg:ASPxPivotGrid><asp:AccessDataSourceID="AccessDataSource1"runat="server"DataFile="~/App_Data/nwind.mdb"SelectCommand="SELECT [Sales Person] AS Sales_Person, [Extended Price] AS Extended_Price, [CategoryName] FROM [SalesPerson]"></asp:AccessDataSource></div></form></body></html>