This example demonstrates how to create a column cell template at runtime. Grid cells display the ASPxHyperLink control created in the template.
Implementation Details
Declare a custom class that implements the ITemplate interface.
In the class's InstantiateIn method, create controls that make up the template and add them to the control collection of a container passed as this method's parameter. The container type is different for each template type. For a DataItemTemplate object, the container is of the GridViewDataItemTemplateContainer type. Use the container's properties to obtain a row's information. For example, use the KeyValue property to get a row key (container.KeyValue
).
Create an instance of the template class and assign it to the column's DataItemTemplate property.
Files to Look At
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- How to: Create Edit Form Templates Dynamically
- How to: Implement a Dynamical DataItem Template for Several Same-type Columns
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HyperlinkColumn._Default" %>
<%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server"
OnLoad="ASPxGridView1_Load">
</dx:ASPxGridView>
</div>
</form>
</body>
</html>
C#using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DevExpress.Web;
namespace HyperlinkColumn {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void ASPxGridView1_Load(object sender, EventArgs e) {
ASPxGridView1.KeyFieldName = "ID";
ASPxGridView1.DataSource = GetData();
if(!IsPostBack && !IsCallback) {
PopulateColumns();
ASPxGridView1.DataBind();
}
else
CreateTemplate();
}
public DataTable GetData() {
DataTable Table = new DataTable();
Table.Columns.Add("ID", typeof(int));
Table.Rows.Add(1);
Table.Rows.Add(2);
return Table;
}
public void PopulateColumns() {
GridViewDataTextColumn colID = new GridViewDataTextColumn();
colID.FieldName = "ID";
ASPxGridView1.Columns.Add(colID);
GridViewDataTextColumn colItemTemplate = new GridViewDataTextColumn();
colItemTemplate.DataItemTemplate = new MyHyperlinkTemplate(); // Create a template
colItemTemplate.Name = "colItemTemplate";
colItemTemplate.Caption = "Template Column";
ASPxGridView1.Columns.Add(colItemTemplate);
}
private void CreateTemplate() {
((GridViewDataColumn)ASPxGridView1.Columns["colItemTemplate"]).DataItemTemplate = new MyHyperlinkTemplate();
}
}
class MyHyperlinkTemplate : ITemplate {
public void InstantiateIn(Control container) {
ASPxHyperLink link = new ASPxHyperLink();
GridViewDataItemTemplateContainer gridContainer = (GridViewDataItemTemplateContainer)container;
link.NavigateUrl = string.Format("~/details.aspx?Device={0}", gridContainer.KeyValue);
link.Text = string.Format("Get details about device {0}", gridContainer.KeyValue);
container.Controls.Add(link);
}
}
}