Page 161 - DCAP312_WEB_TECHNOLOGIES_II
P. 161

Unit 8: Creating More Advanced ASP.NET



            8.2.6 How to Create ASP.NET User Controls                                             Notes
            You create ASP.NET user controls in much the same way that you design ASP.NET Web pages.
            You can use the same HTML elements and controls on a user control that you do on a standard
            ASP.NET page. However, the user control does not have html, body, and form elements; and
            the file name extension must be .ascx.

            To create an ASP.NET user control
               •  Open the Web site project to which you want to add user controls. If you do not already
                 have a Website project, you can create one.

               •  On the Website menu, click Add New Item.
               •  The Add New Item dialog box appears.
               •  In the Add New Item dialog box, under Visual Studio installed templates, click Web User
                 Control.
               •  In the Name box, type a name for the control.
               •  By default, the .ascx file name extension is appended to the control name that you type.

               •  From the Language list, select the programming language that you want to use.
               •  Optionally, if you want to keep any code for the user control in a separate file, select the
                 Place code in separate file check box.
               •  Click Add.

            8.2.7 How to Include ASP.NET User Controls in Web Pages
            Adding an ASP.NET user control to a Web page is similar to adding other server controls to
            the page. However, you must be sure to follow the procedure below so that all of the necessary
            elements are added to the page.

            To add an ASP.NET user control to a Web page
               1.  In Visual Web Developer, open the Web page to which you want to add the ASP.NET
                 user control.

               2.  Switch to Design view.
               3.  Select your custom user control file in Solution Explorer, and drag it onto the page.
            8.2.8 Creating Reusable Elements with ASP.NET User Controls

            ASP.NET user controls let you encapsulate the functionality of multiple server controls in a unit.
            User controls are made up of one or more ASP.NET server controls—Button controls, TextBox
            controls, and so on—along with any code that is required for the controls to perform the function
            that you want to accomplish. The user control can also include custom properties or methods
            that reveal the functionality of the user control to a container, which is typically an ASP.NET
            page. In this walkthrough, you will create an ASP.NET user control that acts as a picker control.
            The picker control has two lists, with a set of choices in one list (the source). Users can select
            items in the SourceList list, and then add the items to the Target List.
            It has three parts, as follows:

               •  In the first part, you will create the basic user control, adding controls and code.
               •  In the second part, you will create a new ASP.NET page (the host page), and then add
                 the user control to the ASP.NET page.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   155
   156   157   158   159   160   161   162   163   164   165   166