Loading Template For Asp.Net Control
First of all here what i've got from
MSDN Magazines
For most of us as web developer , work with asp.net controls, some of these controls let us control the layout, by providing us template, like for example, header template, footer template,item template.
now There are 4 types of how we can load the template
- Manually declare on the tag on aspx page
- Full In Memory template using Class inherit from ITemplate
- Page.LoadTemplate(ascx path)
- Itemplate with Loading from HTML files
Now this can be combine for some scenario ,if user's role is admin load another template.etc.
example no.2:
internal class TypeDescriptiontemplate : ITemplate
{
#region ITemplate Members
public void InstantiateIn(Control container)
{
Label b = new Label();
b.DataBinding += new EventHandler(BindTypeDescription);
container.Controls.Add(b);
}
#endregion
}
static void BindTypeDescription(object sender, EventArgs e)
{
Label l = (Label)sender;
//get the parent container of this sender
GridViewRow container = (GridViewRow)l.NamingContainer;
DataRowView drv = (DataRowView)container.DataItem;
l.Text = drv["Type"] + ", " + drv["Description"];
}
protected void Page_Load(object sender, EventArgs e)
{
//in memory Template using ITemplate
TemplateField b = new TemplateField();
b.HeaderText = "Type Plus Description";
b.ItemTemplate = new TypeDescriptiontemplate();
GridView1.Columns.Add(b);
}
example no.3:
protected void Page_Load(object sender, EventArgs e)
{
//Ascx template
TemplateField c = new TemplateField();
c.HeaderText = "Details";
c.ItemTemplate = Page.LoadTemplate("~/Qty.ascx");
GridView1.Columns.Add(c);
}
Qty.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Qty.ascx.cs" Inherits="testPro35wrox.Qty" %>
<table>
<tr>
<td>
<asp:CheckBox ID="ChkIsStockAvailable" Text="IsOnStock" runat="server" />
</td>
<td style="vertical-align:middle">
<asp:TextBox ID="TxtQty" runat="server"></asp:TextBox>
</td>
<td>
<asp:Image Height="20" Width="20" ID="ImgCollateral" runat="server" />
</td>
</tr>
</table>
example no.4:
the htmlfile
mydatatemplate.htm
<!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>
<title></title>
</head>
<body>
<table style="width: 100%;">
<tr>
<td>
{0}
</td>
<td>
{1}
</td>
<td>
{2}
</td>
<td>
{3}
</td>
</tr>
</table>
</body>
</html>
public class MycustomTemplate : ITemplate
{
public event EventHandler OnPanelBinding;
#region ITemplate Members
public void InstantiateIn(Control container)
{
Panel p = new Panel();
p.DataBinding+=OnPanelBinding;
container.Controls.Add(p);
}
#endregion
}
protected void Page_Load(object sender, EventArgs e)
{
MycustomTemplate dd = new MycustomTemplate();
dd.OnPanelBinding += new EventHandler(p_DataBinding);
DataList1.ItemTemplate = dd;
DataList1.DataSource = SqlDataSource1;
DataList1.DataBind();
}
protected string Merge(string all,string type,string description,string priority)
{
return String.Format(all,type, description, priority,"");
}
public void p_DataBinding(object sender, EventArgs e)
{
string theframe = LoadFrame();
DataListItem container = (DataListItem)((Panel)sender).NamingContainer;
var i = new Literal();
DataRow containerrow =(DataRow)((DataRowView)container.DataItem).Row;
i.Text = Merge(theframe, containerrow["Type"].ToString(),containerrow["Description"].ToString(),containerrow["Priority"].ToString());
((Panel)sender).Controls.Add(i);
}