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. 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. 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 Google.com using the terms in the text box:
Protected Sub btnSearch_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnSearch.Click
Response.Write(Page.IsValid)
Dim queryStr As String = HttpUtility.UrlEncode(txtSearch.Text)
Response.Redirect("http://www.google.com/search?q=" & 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. 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. 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. 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. 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. Examining the two Web Parts in the two WebPartZone
controls
Summary
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) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com.
-
Create Custom SharePoint WebParts, it is simple.
2009-10-01 02:48:06 sarangasl [View]
-
Create Custom SharePoint WebParts, it is simple.
2009-12-17 05:36:44 AbdulSami [View]
-
Accessing webparts again
2008-12-18 12:25:40 amitguitarist [View]
-
Accessing webparts again
2009-06-24 02:05:37 v.vivek [View]
-
Making Google Search functional??
2008-01-03 06:54:43 kencar [View]
-
Making Google Search functional??
2008-01-03 07:44:12 kencar [View]
-
Close
2007-01-24 15:11:37 stanlj [View]
-
Customising Content
2006-07-13 17:01:00 Biggles_on_Tour [View]
-
Webpart demo
2006-06-26 00:07:49 mamboboy [View]
-
Image to open the menu
2006-06-09 04:39:17 Chintan1979 [View]
-
I can't see verbs
2006-05-24 15:48:03 KHuang [View]
-
I can't see verbs
2006-08-14 02:16:50 Arcalis [View]
-
I can't see verbs
2007-08-26 08:43:34 Lepu [View]
-
I can't see verbs
2006-07-14 11:20:34 ShawnZ [View]
-
Where to find the missing images
2005-11-28 07:03:42 murpke [View]
-
Web Parts
2005-08-25 05:08:11 Ravikanth [View]
- Trackback from http://thefrazierclan.org/blogs/andrews_technical_blog/archive/2005/07/21/71.aspx
Web Parts How-To
2005-07-21 05:45:51 [View]
- Trackback from http://weblogs.dotnetforum.dk/guidmaster/archive/2005/06/28/8660.aspx
Igang med ASP.NET Web Parts
2005-06-28 07:36:45 [View]
-
Wish article had separate images....
2005-06-15 11:25:42 CRJones [View]
-
Apparently this demo requires SQL Server Express 2005
2005-05-26 18:16:48 MarkHaliday [View]
-
Apparently this demo requires SQL Server Express 2005
2006-07-05 06:01:14 hasoon [View]
-
Apparently this demo requires SQL Server Express 2005
2005-07-20 05:40:55 Anil'sGround [View]
-
Apparently this demo requires SQL Server Express 2005
2005-05-26 21:28:34 Wei-Meng Lee |
[View]

