Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Building Templated Databound Controls container.Controls.Add(lblTitle); container.Controls.Add(new LiteralControl(“”)); container.Controls.Add(lblAuthor); container.Controls.Add(new LiteralControl(“”)); container.Controls.Add(lblContents); } void lblTitle_DataBinding(object sender, EventArgs e) { Label lblTitle = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault)lblTitle.NamingContainer; lblTitle.Text = container.Title; } void lblAuthor_DataBinding(object sender, EventArgs e) { Label lblAuthor = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault)lblAuthor.NamingContainer; lblAuthor.Text = container.Author; } void lblContents_DataBinding(object sender, EventArgs e) { Label lblContents = (Label)sender; ArticleWithDefault container = ➥(ArticleWithDefault)lblContents.NamingContainer; lblContents.Text = container.Contents; } } } The control in Listing 37.3 is similar to the control created in the previous. | 1654 CHAPTER 37 Building Templated Databound Controls container.Controls.Add lblTitle container.Controls.Add new LiteralControl br container.Controls.Add lblAuthor container.Controls.Add new LiteralControl br container.Controls.Add lblContents void lblTitle_DataBinding object sender EventArgs e Label lblTitle Label sender ArticleWithDefault container ArticleWithDefault lblTitle.NamingContainer lblTitle.Text container.Title void lblAuthor_DataBinding object sender EventArgs e Label lblAuthor Label sender ArticleWithDefault container ArticleWithDefault lblAuthor.NamingContainer lblAuthor.Text container.Author void lblContents_DataBinding object sender EventArgs e Label lblContents Label sender ArticleWithDefault container ArticleWithDefault lblContents.NamingContainer lblContents.Text container.Contents The control in Listing 37.3 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 37.3 implements the ITemplate interface. In particular the class implements the InstantiateIn method which creates all the controls that appear in the template. In Listing 37.3 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 37.4 illustrates how you can use the modified Article control. LISTING 37.4 ShowArticleWithDefault.aspx @ Page Language C @ Register