Skip to main content

How To Create Templated Databound Controls




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DivView.aspx.cs"
Inherits="DivView" %>


<%@ Register Namespace="BoundCustomControl" TagPrefix="custom"
Assembly="DataBoundControl" %>

<!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>
<style type="text/css">
.movies
{
width:500px;
}
.movies div
{
border:solid 1px black;
padding:10px;
margin:10px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<custom:DivView ID="a" runat="server" CssClass="movies">
<ItemTemplate>
<h1>
<%# Eval("Title") %>
</h1>
Director:
<%# Eval("Director") %>
</ItemTemplate>
</custom:DivView>
</div>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DivView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
a.DataSource = GetCustomMadeDataTable();
a.DataBind();
}
public DataTable GetCustomMadeDataTable()
{

//Create a new DataTable object

System.Data.DataTable objDataTable = new System.Data.DataTable();

//Create three columns with string as their type

objDataTable.Columns.Add("Id", typeof(string));
objDataTable.Columns.Add("Title", typeof(string));
objDataTable.Columns.Add("Director", typeof(string));
DataRow dr;
//Adding some data in the rows of this DataTable
for (int i = 0; i <= 50; i++)
{
dr = objDataTable.NewRow();
dr[0] = i.ToString();
dr[1] = "Title" + i.ToString();
dr[2] = "Director" + i.ToString();
objDataTable.Rows.Add(dr);





}

DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["Id"];
objDataTable.PrimaryKey = dcPk;
Session["dtTemp"] = objDataTable;
return objDataTable;
}
}




BoundCustomControl


using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace BoundCustomControl
{
public class DivView : CompositeDataBoundControl
{

private ITemplate _itemTemplate;

[TemplateContainer(typeof(DivViewItem)),
PersistenceMode(PersistenceMode.InnerProperty)]

public ITemplate ItemTemplate
{
get { return _itemTemplate; }
set { _itemTemplate = value; }
}
protected override int CreateChildControls(System.Collections.IEnumerable dataSource,
bool dataBinding)
{
int counter = 0;
foreach (object dataItem in dataSource)
{

DivViewItem contentItem = new DivViewItem(dataItem, counter);
_itemTemplate.InstantiateIn(contentItem);
Controls.Add(contentItem);
counter = counter + 1;
}
DataBind(false);
return counter;

}
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Div; }
}

}
public class DivViewItem : WebControl, IDataItemContainer
{
private object _dataItem;
private int _index;

public DivViewItem(object dataItem, int index)
{
_dataItem = dataItem;
_index = index;


}


object IDataItemContainer.DataItem
{
get { return _dataItem; }
}

int IDataItemContainer.DataItemIndex
{
get { return _index; }
}

int IDataItemContainer.DisplayIndex
{
get { return (int)_index; }
}

protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}

}

}

Comments

Popular posts from this blog