home *** CD-ROM | disk | FTP | other *** search
-
- <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
-
- <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
-
- <h4>Web Forms Pagelet Controls</h4>
- <p>
-
- <!-- #include virtual="/quickstart/aspplus/include/wftoc4-2.inc" -->
- <p>
- <hr>
-
-
- <!--BEGIN SECTION-->
- <a name="intro">
- <span class="subhead">Introduction to Pagelets</span>
- <p>
- In addition to the built-in server controls provided by ASP+, you can easily define your own controls using the same programming
- techniques you've already learned for writing Web Forms. In fact, with just a few modifications, almost any Web Form
- can be re-used in another page as a server control (notice that tha base System.Web.UI.Page class inherits directly from
- System.Web.UI.Control). A Web Form used as a server control is named a <b>Pagelet</b>,
- for short. As a matter of convention, we use the <b>.aspc</b> extension to indicate such controls. This ensures that the Pagelet's file cannot
- be executed as a standalone Web Form, if we so desire (we'll see a little later how there are a few, albiet important, differences between
- a Pagelet and a Web Form). Pagelets are included into another Web Form using a <b>Register</b> directive:
-
- <div class="code"><pre>
- <%@ Register TagPrefix="Acme" TagName="Message" Src="pagelet1.aspc" %>
- </pre></div>
-
- <p>
- The TagPrefix determines a unique namespace for the Pagelet (so that multiple Pagelets with the same name can be differentiated from
- each other). The TagName is the unique name for the Pagelet (you can choose any name). The Src attribute is the virtual path to the
- Pagelet, for example "MyPagelet.aspc" or "/MyApp/Include/MyPagelet.aspc". After registering the Pagelet, you may place the Pagelet
- tag in the Web Form just as you would an ordinary server control (including the runat="server" attribute):
- <p>
-
- <div class="code"><pre>
- <Acme:Message runat="server"/>
- </pre></div>
-
- <p>
- The following example shows a Pagelet imported into another Web Form. Note that the Pagelet in this case is just a simple static file:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet1.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet1.src"
- Icon="/quickstart/aspplus/images/pagelet1.gif"
- Caption="Pagelet1.aspx"
- runat="server" />
-
-
- <!--BEGIN SECTION-->
- <br>
- <a name="properties">
- <br>
- <span class="subhead">Exposing Pagelet Properties</span>
- <p>
-
- When we treat a Web Form as a control, the public fields and methods of that Web Form are promoted to public properties (i.e. tag
- attributes) and methods of the control as well. The following example shows an extension of the previous Pagelet example that adds
- two public String fields. Notice that these fields can be set either declaratively or programmatically in the containing page.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet2.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet2.src"
- Icon="/quickstart/aspplus/images/pagelet2.gif"
- Caption="Pagelet2.aspx"
- runat="server" />
-
- <p>
- In addition to promoting public fields to control properties, the property syntax may be used. Property syntax has the advantage
- of being able to execute code when properties are set or retrieved. In this next example, we've implemented an Address Pagelet that wraps the "Text"
- properties of TextBox controls within it. The benefit of doing this is that we inherit the automatic state management of the TextBox control
- for free!
- <p>
- Notice that we've placed two Address Pagelets on the containing Web Form, and differentiated between them by setting the
- "caption" property to "Billing Address" and "Shipping Address" respectively. The real power of Pagelets is in this type of reusability.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet3.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet3.src"
- Icon="/quickstart/aspplus/images/pagelet3.gif"
- Caption="Pagelet3.aspx"
- runat="server" />
-
- <p>
- Another useful Pagelet is a Login control for collecting usernames and passwords:
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet4.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet4.src"
- Icon="/quickstart/aspplus/images/pagelet4.gif"
- Caption="Pagelet4.aspx"
- runat="server" />
-
- <p>
- Here we've added form validation controls to the Login Pagelet.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet5.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet5.src"
- Icon="/quickstart/aspplus/images/pagelet5.gif"
- Caption="Pagelet5.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <br>
- <a name="events">
- <br>
- <span class="subhead">Encapsulating Events in a Pagelet</span>
- <p>
-
- Pagelets participate in the complete execution lifecycle of the request, much the way ordinary server controls do. This means that
- a Pagelet can handle its own events, encapsulating some of the page logic from the containing Web Form. In this next example,
- we've implemented a product listing Pagelet that internally handles its own post-backs. One thing to notice is that the Pagelet itself
- has no wrapping <form runat="server"> control. Because only one form control may be present on a page (ASP+ does not
- allow nested server forms), we leave it to the containing Web Form to define this for us.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet6.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet6.src"
- Icon="/quickstart/aspplus/images/pagelet6.gif"
- Caption="Pagelet6.aspx"
- runat="server" />
-
-
- <!--BEGIN SECTION-->
- <br>
- <a name="program">
- <br>
- <span class="subhead">Creating Pagelets Programmatically</span>
- <p>
-
- Just as ordinary server controls can be created programmatically, so too can Pagelets. We use the Page's <b>LoadControl</b> method to
- load the Pagelet, passing the virtual path to the Pagelet's source file:
-
- <div class="code"><pre>
- Control c1 = LoadControl("pagelet7.aspc");
- ((pagelet7_aspc)c1).Category = "business";
- Page.Controls.Add(c1);
- </pre></div>
-
- The type of the Pagelet is determined by the ASP+ runtime, following the convention <i>filename</i>_<i>extension</i>.
- For example, a Pagelet saved as filename "pagelet7.aspc" is assigned the strong type "pagelet7_aspc". Because the LoadControl
- method returns a type of System.Web.UI.Control, we need to cast to the appropriate strong type in order to set individual properties
- of the Pagelet control. Finally, the Pagelet is added to the base Page's Controls collection.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/pagelets/Pagelet7.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/pagelets/Pagelet7.src"
- Icon="/quickstart/aspplus/images/pagelet7.gif"
- Caption="Pagelet7.aspx"
- runat="server" />
-
- <p>
- <b>Important: </b> The strong type for a Pagelet is available to the containing Web Form only if a Register directive is included for
- the Pagelet (even if there are no Pagelet tags actually declared).
-
- <!--BEGIN SECTION-->
- <a name="endofsection">
-
- <h4>Section Summary</h4>
- <ol>
- <li>Pagelets allow developers to easily define custom controls using the same programming techniques for writing Web Forms.
- <li>As a matter of convention, we use the <b>.aspc</b> extension to indicate such controls. This ensures that the Pagelet's file cannot
- be executed as a standalone Web Form.
- <li>Pagelets are included into another Web Form using a <b>Register</b> directive, which specifies a TagPrefix, TagName, and Src location.
- <li>After registering the Pagelet, a Pagelet tag may be placed in a Web Form as an ordinary server control (including the runat="server" attribute).
- <li>The public fields, properties and methods of a Pagelet are promoted to public properties (i.e. tag attributes) and methods of the
- control in the containing Web Form.
- <li>Pagelets participate in the complete execution lifecycle of every request and can handle their own events, encapsulating some of the
- page logic from the containing Web Form.
- <li>Pagelets should not contain any form controls, but should instead rely on their containing Web Form to include one if necessary.
- <li>Pagelets may be created programmatically using the LoadControl method of the System.Web.UI.Page class. The type of the Pagelet is
- determined by the ASP+ runtime, following the convention <i>filename</i>_<i>extension</i>.
- <li>The strong type for a Pagelet is available to the containing Web Form only if a Register directive is included for
- the Pagelet (even if there are no Pagelet tags actually declared).
- </ol>
- <p>
-
- <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->