8.3 提供默认模板
为了简化模板控件的使用,很多模板控件都提供了默认模板。比如,Login控件提供了LoginTemplate,如果用户没有设置LayoutTemplate模板,则使用LoginTemplate作为默认模板。
默认模板就是一个实现了ITemplate接口的类,需要在它的InstantiateIn()方法中组织默认模板内容。
接下来我们实现具有默认模板功能的BookInfo控件。
8.3.1 DefaultBookInfo
实现具有默认模板的BookInfo控件,需要做两件事件,首先实现一个实现了ITemplate接口的默认模板类,然后重写CreateChildControls()方法,在模板为空时使用默认模板。
public class DefaultBookInfo:BookInfo
{
protected override void CreateChildControls()
{
if (ItemTemplate == null)
{
ItemTemplate = new BookInfoDefaultTemplate();
}
ItemTemplate.InstantiateIn(this);
}
}
public class BookInfoDefaultTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
HtmlGenericControl divContainer = new HtmlGenericControl("div");
divContainer.Attributes["class"] = "bookContainer";
container.Controls.Add(divContainer);
Image img = new Image();
img.ID = "imgBook";
img.CssClass = "bookCover";
img.DataBinding += new EventHandler(img_DataBinding);
divContainer.Controls.Add(img);
HtmlGenericControl divContent = new HtmlGenericControl("div");
divContent.Attributes["class"] = "bookContent";
divContainer.Controls.Add(divContent);
HtmlGenericControl h3 = new HtmlGenericControl("h3");
divContent.Controls.Add(h3);
Literal ltlTitle = new Literal();
ltlTitle.DataBinding += new EventHandler(ltlTitle_DataBinding);
h3.Controls.Add(ltlTitle);
Literal ltlContent = new Literal();
divContent.Controls.Add(ltlContent);
ltlContent.DataBinding += new EventHandler(ltlContent_DataBinding);
HtmlGenericControl divPublisher = new HtmlGenericControl("div");
divPublisher.Attributes["class"] = "bookPublisher";
divContent.Controls.Add(divPublisher);
Literal ltlPublisher = new Literal();
ltlPublisher.DataBinding +=
new EventHandler(ltlPublisher_DataBinding);
divPublisher.Controls.Add(ltlPublisher);
}
void ltlPublisher_DataBinding(object sender, EventArgs e)
{
Literal ltl = (Literal)sender;
DefaultBookInfo container = (DefaultBookInfo)ltl.NamingContainer;
ltl.Text = container.DataItem.Publisher;
}
void ltlContent_DataBinding(object sender, EventArgs e)
{
Literal ltl = (Literal)sender;
DefaultBookInfo container = (DefaultBookInfo)ltl.NamingContainer;
ltl.Text = "Written by:" + container.DataItem.Author
+ "<br/>ISBN:#" + container.DataItem.ISBN;
}
void ltlTitle_DataBinding(object sender, EventArgs e)
{
Literal ltl = (Literal)sender;
DefaultBookInfo container = (DefaultBookInfo)ltl.NamingContainer;
ltl.Text = container.DataItem.Title;
}
void img_DataBinding(object sender, EventArgs e)
{
Image img = (Image)sender;
DefaultBookInfo container = (DefaultBookInfo)img.NamingContainer;
img.AlternateText = container.DataItem.Title;
img.ImageUrl = "~/images/" + container.DataItem.ISBN + ".png";
}
}
在BookInfoDefaultTemplate类的InstantiateIn()方法中,逐步创建默认模板所需的子控件,并把它们添加到容器,也就是DefaultBookInfo控件的Controls集合中,默认模板中的不少子控件都注册了DataBinding事件处理程序,在调用DefaultBookInfo控件的DataBind()方法时,这些子控件的DataBinding事件会触发,因此子控件得以绑定父控件的数据。
这样,DefaultBookInfo控件不需要提供ItemTemplate就能显示图书信息,如图8-5所示。
<tc:DefaultBookInfo ID="DefaultBookInfo1" runat="server">
</tc:DefaultBookInfo>

图8-5 DefaultBookInfo的默认模板效果







