oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Building Web Parts, Part 1
Pages: 1, 2

Creating Web Parts Using Web User Control

Let's take a look at the second method of adding a Web Part to a portal page: creating a Web User control from scratch and dropping it onto a WebPartZone. The control we're going to build will allow users to search Google.

1. First you need to create a new control. Add a new Web User control to your project by right-clicking on the project name in Solution Explorer and then selecting Web User Control. Name the Web User control "Google.ascx."

2. Since we're going to display the Google logo to identify our Web Part, we need to create a folder to store the image. Add a new folder to the project and name it Images (right-click on the project name in Solution Explorer and select Add Folder -> Regular Folder). Save the Google.gif image in the C:\ Webparts1\Images folder (see Figure 6).

Figure 6
Figure 6. The image in the Images folder

3. Now you need to implement your control. First, insert a 2-by-2 table into the Web User control (Layout -> Table) to organize its contents. Add an Image control, a Textbox control, and a Button control to three of the table cells, as shown in Figure 7. Associate Google.gif with the Image control and add the word "Search" to the Button control.

Figure 7
Figure 7. Populating the Web User control with the various controls

4. It's time to add the code that will carry out a search when the user enters a search term in the text box and clicks the Search button. Double-click on the Search button and type the following code, which will send a search query to using the terms in the text box:

Protected Sub btnSearch_Click(ByVal sender As Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnSearch.Click
  Dim queryStr As String = HttpUtility.UrlEncode(txtSearch.Text)
  Response.Redirect("" & queryStr)
End Sub

5. You can now drag and drop the Google.ascx Web User control from the Solution Explorer onto the WebPartZone2 control in Default.aspx (see Figure 8).

Figure 8
Figure 8. Dragging and dropping a Web User control onto a WebPartZone control

6. Note that the Google control is untitled. To add a title, switch to Source View and add the <title> attribute to the newly added Web User control, assigning it the value "Google Search":

<uc1:Google title="Google Search" runat="server" ID="Google1" />

7. To see how the Web User control looks in IE, press F5. Your portal page should now resemble the one shown in Figure 9.

Figure 9
Figure 9. Displaying the two Web Parts on the page

Customizing the Look and Feel of Web Parts

By default, the look and feel of Web Parts controls is determined by the default layout set by the WebPartZone control. By default, Web Parts display their various options (such as Minimize, Close, etc.) as hyperlinks. You adjust the look and feel of Web Parts by altering the properties of the containing WebPartZone control, and you can customize the look of the option links by adding your own images. Let's see how you can do that.

Remember, the settings in a WebPartZone control affect the WebPart controls it contains. So you have to set each WebPartZone control in order to adjust the look and feel of its Web Parts.

1. First, you'll need some images to use as icons. Add the images shown in Figure 10 to the Images folder of your project.

Figure 10
Figure 10. The images stored in the Images folder

2. Now you'll want to assign each of the images to one of the option links. Select the WebPartZone1 control and view its Properties window. Locate the following properties and set their ImageURL properties as follows::

  • CloseVerb.ImageUrl="Images/CloseVerb.gif"
  • EditVerb.ImageUrl="Images/EditVerb.gif"
  • MinimizeVerb.ImageUrl="Images/MinimizeVerb.gif"
  • RestoreVerb.ImageUrl="Images/RestoreVerb.gif"

3. Apply the Professional scheme to the WebPartZone1 control by clicking on the "Auto Format..." link in the WebPartZone Tasks menu (see Figure 11). Likewise, apply the Colorful scheme to the WebPartZone2 control.

Settings applied to a WebPartZone control affect all the Web Parts controls contained in that Web Part zone. If a Web Part is moved from one Web Part zone into another, it will assume the behavior of the Web Part zone it is in.

Figure 11
Figure 11. Apply a scheme to the WebPartZone control

4. Press F5 to test the application. You will realize that the Calendar control in the first WebPartZone control has icons displayed next to the Minimize and Close links. In contrast, the Web User control in the second WebPartZone control does not have the icons (see Figure 12). This is because you only configured the first WebPartZone control to display icons. In the next article, you will learn how to allow your user to rearrange the various Web Parts on the page.

Figure 12
Figure 12. Examining the two Web Parts in the two WebPartZone controls


In this article, you have seen how to create new Web parts using Web server controls as well as Web User controls. In the next article, you will see how to move the Web Parts from one WebPartZone into another.

Wei-Meng Lee (Microsoft MVP) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies.

Return to