Exam 70-553 - Create a composite Web application control.
Section 2
-
Part 3
Creating Custom Web Controls
-
Topic 1
-
Create a composite Web application control.
- Create a user control.
- Convert a Web Forms page to a user control.
- Include a user control in a Web Forms page.
- Manipulate user control properties.
- Handle user control events within the user control code-declaration block or code-behind file.
- Create instances of user controls programmatically.
- Develop user controls in a code-behind file.
- Create a templated user control.
Summary
User Controls are reusable components that can be added to a web page. You can create them by adding them to your project and registering them on your page and then adding a tag with the same tag prefix you registered with the name of the control.
You can convert a web forms page to a user control by removing the html, head, body, and form tags, Change the Page Directive to the Control directive and changing the extension to ascx,
User Control Properties can be modified in code as long as the type has been cast back to the type that the control is. User Controls can manage their events the same way a page would and can even expose events so that pages that host the control can respond to those events.
You can add User Controls Programattically by Calling the LoadControl Method to initialize a Control and then call the containers AddControl method to add the newly created control to the page. Below is an example taken from the ASP.Net Quickstart Tutorial mentioned in the resources:
Control c1 = LoadControl("userctrl7.ascx");
((UserCtrl7)c1).Category = "business";
Page.Controls.Add(c1);or
UserCtrl7 c1 = LoadControl("userctrl7.ascx");
c1.Category = “business”;Page.Controls.Add(c1);
To have your UserControls developed using Code Behind files, use partial classes. Using partial class you do not need to declare the controls on your page when they are in the visual ascx file. Read the resources on code behind for details.
To create a templated User Control follow the steps below (taken from Creating a Templated User Control in the resources)
In the .ascx file, declaratively create an ASP.NET Placeholder Web server control where you want the template to appear.
In the user control's code-declaration block or code-behind class, implement a property of type ITemplate.
In the same code block, define a server control that implements INamingContainer as a container in which to create an instance of the template. This is called the template's naming container.
Note This control essentially becomes a nested class of the user control, though this is not required.
Apply the TemplateContainerAttribute to the ITemplate property and pass the type of the template's naming container as the argument to the constructor or the attribute.
In the Page Init method, repeat the following steps one or more times:
Create an instance of the naming container class.
Create an instance of the template in the naming container.
Add the naming container instance to the Controls property of the PlaceHolder server control.
Other Resources & Links:
User Controls – ASP.Net 2.0 Quickstart Tutorial
http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/userctrl/default.aspx
Web User Controls (Includes Section on Converting a Web Forms Page to a User Control)
http://www.startvbdotnet.com/aspsite/extras/user1.aspx
Codebehind and Compilation in ASP.Net 2.0
http://msdn.microsoft.com/msdnmag/issues/06/01/ExtremeASPNET/default.aspx
Creating a Templated User Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingtemplatedusercontrol.asp