Exposing Properties and Methods in User Controls


Exposing Properties and Methods in User Controls

In the preceding section, you learned how to use a user control to display a standard header on multiple ASP.NET pages. There is one problem with this header user control, however ”it displays the exact same page title on each page.

Fortunately, you can find an easy way around this problem, and the solution illustrates the flexibility and power of user controls. User controls do not need to be simple static files. You can expose properties in a user control and set and read these properties in the containing page.

You can, for example, expose a property in the header user control that represents the title of the page. The modified header user control in Listing 5.5 demonstrates how you can do so.

Listing 5.5 HeaderTitle.ascx
 <Script Runat="Server"> Public PageTitle As String = "Global Super Company" </Script> <html> <head><title><%=PageTitle%></title></head> <body> <h2>Global Super Company</h2> <i>We mean business!</i> <hr> 

The C# version of this code can be found on the CD-ROM.

The header user control in Listing 5.5 contains a script that declares a public variable named PageTitle . Because the variable is declared as public, it can be accessed within any page that contains the user control.

The PageTitle variable displays the title of the page. The value of the variable is rendered within the opening and closing <title> tags. The variable is given a default value of Global Super Company . If no other value is assigned to the variable, it displays this default title.

Now that you've declared the public variable, you can set the variable in any page that contains the user control, as illustrated in Listing 5.6.

Listing 5.6 HomePageTitle.aspx
 <%@ Register TagPrefix="SuperCompany" TagName="Header"   Src="HeaderTitle.ascx" %> <Supercompany:Header   ID="ctlHeader"   PageTitle="The Home Page"   Runat="Server" /> Welcome to our home page! </body> </html> 

The C# version of this code can be found on the CD-ROM.

Notice how a value is assigned to the header user control when the user control tag is declared. The value is assigned within the declaration of the header user control. When the page in Listing 5.6 is displayed, the title of the page is The Home Page.

You also can assign a value to the PageTitle user control property programmatically. Suppose that you want to display the current date in the page title. The page in Listing 5.7 assigns a value to the PageTitle variable in the Page_Load subroutine.

Listing 5.7 HomePageDate.aspx
 <%@ Register TagPrefix="SuperCompany" TagName="Header"   Src="HeaderTitle.ascx" %> <Script Runat="Server"> Sub Page_Load   ctlHeader.PageTitle = "Date: " & Now.ToString( "D" ) End Sub </Script> <Supercompany:Header   ID="ctlHeader"   PageTitle="The Home Page"   Runat="Server" /> Welcome to our home page! </body> </html> 

The C# version of this code can be found on the CD-ROM.

All public variables declared in the user control file are exposed as properties of the user control. Functions and subroutines contained in the user control file can also be exposed. However, functions and subroutines are exposed not as properties, but as methods of the user control.

The user control contained in Listing 5.8, for example, repeats a word a certain number of times. It has one property called Word , which represents the word to be repeated, and one method called Repeat() , which causes the word to repeated.

Listing 5.8 WordRepeater.ascx
 <Script Runat="Server"> Public Word As String Sub Repeat( intNumTimes As Integer )   Dim intCounter As Integer   For intCounter = 0 to intNumTimes - 1     Response.Write( Word )   Next End Sub </Script> 

The C# version of this code can be found on the CD-ROM.

The Repeat() method is implemented in the user control as a subroutine. It's a subroutine that has one parameter that represents the number of times the word should be repeated.

The page in Listing 5.9 demonstrates how you call the Repeat() method to display the word Hello! 50 times.

Listing 5.9 DisplayWordRepeater.aspx
 <%@ Register TagPrefix="SuperCompany" TagName="WordRepeater"   Src="WordRepeater.ascx" %> <Script Runat="Server"> Sub Page_Load   myWordRepeater.Repeat( 50 ) End Sub </Script> <html> <head><title>DisplayWordRepeater.aspx</title> <body> <SuperCompany:WordRepeater   ID="myWordRepeater"   Word="Hello"   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

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