Creating User Controls

Creating Web Forms should be fresh in your mind (recall that we created several forms in Chapter 5), so it seems worthwhile to start a discussion of creating controls with a very similar process of creating a UserControl-derived object and including it on a test page. A user control can be created in two ways. The first, and perhaps the simplest, technique is to create a page with the attributes and controls you want on the user control and then convert the page to a user control. The second technique is to create the user control programmatically and then test it on another page. In general, I prefer to create the user control as a regular Web page and then modify it for use as a user control. In any event, the result will be a file with an .ascx extension that contains the code on the page or that contains an Src attribute pointing to a code-behind file containing a class derived from UserControl. The .ascx file will contain an @ Control directive rather than an @ Page directive.

0 0

Post a comment