Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit

Working with a Master Page's Source Code Portion

A master page has all of the functionality found in a standard ASP.NET page. That means that it can have Web controls and static HTML markup. The Web controls in the master page can collect user input or retrieve data from a data source. For example, the master page created in the Creating a Master Page section had SiteMapDataSource and TreeView controls, which queried and displayed the site's structure based on the site map. We could have also added controls that collect user input. For example, sites that support user accounts often allow visitors to log in from any page on the site. We could accomplish this functionality by adding a Login control to the master page.

In addition to Web controls and static HTML markup, a master page can also have a server-side source code portion. This source code portion can contain event handlers for the Web controls added to a master page or code that is to run each time a page that inherits the master page is visited. To illustrate the server-side source code portion capabilities of master pages, let's create a new master page for our site, called CodeDemo.master.

In this master page, let's first add a Label Web control that displays the current date and time along with a mechanism to search the Internet from our site. Start by adding the Label Web control that will display the current date and time. Add this control above the ContentPlaceHolder control, clear out its Text property, and set its ID property to currentDateTime. Next, type in the word Search and, after that, add a TextBox Web control, setting its ID to searchTerm. Finally, add a Button Web control after the TextBox, setting its ID and Text properties to btnSearch and Search, respectively. After you have completed these steps, your screen should look similar to Figure 21.10.

Figure 21.10. The master page includes three Web controls: a Label, a TextBox, and a Button.

With these Web controls in place, we're ready to add the server-side source code for the master page. Start by creating the Page_Load event handler. Recall from Hour 2, "Understanding the ASP.NET Programming Model," that the Page_Load event handler fires each and every time the page is visited. In this event handler we'll set the currentDateTime Label's Text property to the current date and time.

To create the Page_Load event handler, double-click in the Design view or go to the source code portion and select from the left drop-down list the (Page Events) option and, from the right drop-down list, the Load event. Add the following line of code to the Page_Load event handler:

currentDateTime.Text = "It is now " & DateTime.Now

For the search interface, when the user enters a search term and clicks the Search button, a postback will ensue, and the Button Web control's Click event will fire. For this example let's have the user search the Internet using Google. This can be accomplished by sending the user to http://www.google.com/search?q=searchTerm. To accomplish this, we'll create an event handler for the Button's Click event and then use Response.Redirect(url) to send the user to Google's site, passing in the user's search term through the querystring.

Start by creating an event handler for the Button Web control's Click event. You can either double-click the Button in the Design view or select the appropriate control and event from the drop-down lists at the top of the source code portion. After you have created the event handler, add the following line of code:

Response.Redirect("http://www.google.com/search?q=" & searchTerm.Text)

Listing 21.5 contains the complete source code portion for the master page.

Listing 21.5. The Master Page Has Two Event Handlers

[View full width]

1: Partial Class CodeDemo 2: Inherits System.Web.UI.MasterPage 3: 4: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 5: currentDateTime.Text = "It is now " & DateTime.Now 6: End Sub 7: 8: Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click 9: Response.Redirect("http://www.google.com/search?q=" & searchTerm.Text) 10: End Sub 11: End Class

By the Way

You can use Google to search just the pages on your site by prepending the user-entered search term with site:yourDomain+. For example, if you wanted to search just www.YourSite.com for the term lunch, you could use the URL http://www.google.com/search?q=site:www.YourSite.com+lunch. Therefore, you can allow users to search just your site (versus searching the entire Internet) by changing line 9 in Listing 21.4 to

[View full width]

Response.Redirect("http://www.google.com/search?q=site:www .YourSite.com+" & searchTerm.Text)

Testing the Master Page's Functionality

At this point we have created a master page that displays the current date and time along with an interface for searching the Web through Google. Unfortunately, we've yet to test this master page's functionality in a browser. To do so, we must create an ASP.NET page that inherits from this new master page.

Take a moment to create a new ASP.NET page, using CodeDemo.master as its master page. Add a short blurb to the Content region in the ASP.NET page and then load it in a browser. As Figure 20.11 shows, when visiting the page, you'll see the current date and time displayed at the top of the page. Furthermore, when you enter a search term into the text box and click the button, you are whisked to Google's search results for the entered search term. Figure 20.12 shows a user's browser after he has entered the term Scott Mitchell into the search text box and clicked the button.

Figure 21.11. The current date and time is displayed at the top of the page.

Figure 21.12. Users can enter a search term and be taken to Google's results page.

Категории