TAILIEUCHUNG - ASP.NET 4 Unleased - p 169

Building Templated Databound Controls (lblTitle); (new LiteralControl(“”)); (lblAuthor); (new LiteralControl(“”)); (lblContents); } void lblTitle_DataBinding(object sender, EventArgs e) { Label lblTitle = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault); = ; } void lblAuthor_DataBinding(object sender, EventArgs e) { Label lblAuthor = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault); = ; } void lblContents_DataBinding(object sender, EventArgs e) { Label lblContents = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault); = ; } } } The control in Listing is similar to the control created in the previous. | 1654 CHAPTER 37 Building Templated Databound Controls lblTitle new LiteralControl br lblAuthor new LiteralControl br lblContents void lblTitle_DataBinding object sender EventArgs e Label lblTitle Label sender ArticleWithDefault container ArticleWithDefault void lblAuthor_DataBinding object sender EventArgs e Label lblAuthor Label sender ArticleWithDefault container ArticleWithDefault void lblContents_DataBinding object sender EventArgs e Label lblContents Label sender ArticleWithDefault container ArticleWithDefault The control in Listing is similar to the control created in the previous section however the CreateChildControls method has been modified. The new version of the CreateChildControls method tests whether there is an ItemTemplate. If no ItemTemplate exists an instance of the ArticleDefaultTemplate class is created. The ArticleDefaultTemplate class which is also included in Listing implements the ITemplate interface. In particular the class implements the InstantiateIn method which creates all the controls that appear in the template. In Listing three Label controls are created that correspond to the Title Author and Contents properties. The DataBinding event is handled for all three of these Label From the Library of Wow eBook Creating Templated Controls 1655 controls. When the DataBind method is called the DataBinding event is raised for each child control in the Article control. At that time the values of the Title Author and Contents properties are assigned to the Text properties of the Label controls. The page in Listing illustrates how you can use the modified Article control. LISTING @ Page Language C @ Register