An Application-Specific User Control: The CruiseSelector Example


An Application-Specific User Control: The CruiseSelector Example

Our next user control sample, CruiseSelector , is an application-specific user control that is not generic like the SiteHeader and RequiredTextField user controls we developed earlier in the chapter. Application-specific user controls are useful because they allow pages to be partitioned into smaller units of functionality.

CruiseSelector uses declarative data-binding page syntax and handles events raised by its constituent controls, demonstrating that user controls can use templates and data-binding syntax, perform event handling, and use other page-programming elements in much the same fashion as pages do. Cruise ­Selector contains a DataList control and a Label control. The DataList displays a list of ASP.NET dinner cruises. When the user selects a cruise and submits the page, the DataList raises an ItemCommand event on the server, which CruiseSelector handles by displaying the cruise selection and disabling the DataList control.

Listing 4-11 contains the source code for CruiseSelector .

Listing 4-11 CruiseSelector.ascx
 <%@ControlLanguage="c#"ClassName="CruiseSelector"%> <scriptrunat="server"> privatevoidLoadDataSource(){ ArrayListlist=newArrayList(); list.Add("ProgrammingASP.NETPages"); list.Add("ASP.NETTipsandTricks"); list.Add("ASP.NETWebServices"); list.Add("AuthoringServerControls101"); list.Add("ProfessionalServerControls"); myList.DataSource=list; } voidPage_Load(objectsender,EventArgse){ if(!IsPostBack){ label1.Text= "LastchancetosignupforanASP.NETdinnercruise!!"; LoadDataSource(); myList.DataBind(); } } //ThehandlerfortheItemCommandeventoftheDataList. privatevoidMyList_ItemCommand(objectsource, DataListCommandEventArgse) { Labell=(Label)e.Item.FindControl("label"); strings="Youareregisteredforthe" +"<b>"+l.Text +"</b>dinnercruise."; label1.Text=s; myList.Enabled=false; } </script> <p/> <asp:Labelid="label1"Font-Name="Verdana"Font-Size="12pt" runat="server"/> 
 <p/> <asp:DataListrunat="server"id="myList"Font-Name="Verdana" Font-Size="16pt"BorderColor="Gray"BorderWidth="1px" CellSpacing="0"CellPadding="2"GridLines="Both" onItemCommand="MyList_ItemCommand"> <HeaderStyleForeColor="Black"BackColor="LightBlue" HorizontalAlign="Center"/> <ItemStyleForeColor="Black"BackColor="#EEEEEE" HorizontalAlign="Right"/> <AlternatingItemStyleBackColor="#DCDCDC"/> <SelectedItemStyleForeColor="White"BackColor="#000084"/> <HeaderTemplate> <asp:Labelid="headerLabel"runat="server" Text="ASP.NETDinnerCruises"/> </HeaderTemplate> <ItemTemplate> <asp:Labelid="label"runat="server" Text='<%#Container.DataItem%>'/> &nbsp;&nbsp; <asp:Buttonbutton="button"runat="server"id="selectButton" CommandName="Select"Text="Select"ForeColor="Blue"/> </ItemTemplate> </asp:DataList> </p> 

A user control can handle the events raised by its child controls, as Cruise ­Selector does, and it can also bubble those events up to its parent control. We will describe event bubbling in Chapter 12, "Composite Controls." In addition to handling or bubbling events raised by its child controls, a user control can also raise events of its own, as any other server control would. We'll discuss events in server controls in Chapter 9, "Control Life Cycle, Events, and Postback."

The ASP.NET page in Listing 4-12 uses the CruiseSelector user control. Note that the page does very little work; most of the work is done by the user control.

Listing 4-12 CruiseSelectorTest.aspx
 <%@PageLanguage="C#"%> <%@RegisterTagPrefix="mspuc"Tagname="CruiseSelector" src="UserControls/CruiseSelector.ascx"%> <body> <formrunat="server"> <p/> <asp:LabelText="Welcometo.NETCruiseRegistration" runat="server"Font-Name="Verdana"Font-Size="20pt" BorderColor="Gray"BorderWidth="2px"ForeColor="Black" BackColor="LightGray"HorizontalAlign="Center"/> <mspuc:CruiseSelectorid="selector"runat="server"/> 
 <p/> </form> </body> </html> 

Figures 4-2 and 4-3 show CruiseSelectorTest.aspx as viewed from a Web browser on first request and on postback.

Figure 4-2. CruiseSelectorTest.aspx viewed in a browser on first request

graphics/f04hn02.jpg

Figure 4-3. CruiseSelectorTest.aspx viewed in a browser on postback

graphics/f04hn03.jpg



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net