Page 159 - DCAP312_WEB_TECHNOLOGIES_II
P. 159

Unit 8: Creating More Advanced ASP.NET



            To convert a single-file ASP.NET Web page into a user control                         Notes
               1.  Rename the control so the file name extension is .ascx.

               2.  Remove the html, body, and form elements from the page.
               3.  Change the @ Page directive to a @ Control directive.

               4.  Remove all attributes of the @ Control directive except Language, AutoEventWireup (if
                 present), CodeFile, and Inherits.

               5.  Include a className attribute in the @ Control directive. This allows the user control to
                 be strongly typed when it is added to a page.

            To convert a code-behind ASP.NET Web page into a user control
               1.  Rename the .aspx file so the file name extension is .ascx.

               2.  Rename the code-behind file to have the file name extension .ascx.vb or .ascx.cs, depending
                 on what programming language the code-behind file is in.

               3.  Open the code-behind file and change the class from which it inherits from Page to User
                 Control.

               4.  In the .aspx file, do the following:
                  •  Remove the html, body, and form elements from the page.

                  •  Change the @ Page directive to an @ Control directive.
                  •  Remove all attributes of the @ Control directive except Language, AutoEventWireup
                    (if present), CodeFile, and Inherits.
                  •  In the @ Control directive, change the CodeFile attribute to point to the renamed
                    code-behind file.
               5.  Include a className attribute in the @ Control directive. This allows the user control to
                 be strongly typed when it is added to a page.
            8.2.3 How to Include a User Control in an ASP.NET Web Page

            To use a user control, you include it in an ASP.NET Web page. When a request arrives for a
            page and that page contains a user control, the user control goes through all of the processing
            stages that any ASP.NET server control performs.
            To include a user control in a Web Forms page
              1.  In the containing ASP.NET Web page, create an @ Register directive that includes:

                  •  A Tag Prefix attribute, which associates a prefix with the user control. This prefix
                    will be included in opening tag of the user control element.
                  •  A Tag Name attribute, which associates a name with the user control. This name will
                    be included in the opening tag of the user control element.
                  •  A Src attribute, which defines the virtual path to the user control file that you are
                    including.
               2.  In the body of the Web page, declare the user control element inside the form element.

               3.  Optionally, if the user control exposes public properties, set the properties declaratively.




                                             LOVELY PROFESSIONAL UNIVERSITY                                   153
   154   155   156   157   158   159   160   161   162   163   164