AddThis Social Bookmark Button

Listen Print Discuss

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 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
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

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.



  • Apparently this demo requires SQL Server Express 2005
    2005-07-20 05:40:55  Anil'sGround [Reply | View]

    Hey Mark,
    The bad thing about VS 2005 beta 2 is that you should install the Sql server CTP(2005) which is along with the build or else it would give you the above error
    The reason what i think is that by default the datasource is Access for VS2005 and this DB is some sought of encrypted format and only those drivers which are a part of sql 2005 CTP would be able to open it.hence its very much mandatory to install Sql 2005.
    hope this would help you to trouble shoot this...if SQL server is the required Database then you would have to run the aspnet_regsql.exe from the winnt/framework/2.XX/ folder.
  • Wei-Meng Lee photo Apparently this demo requires SQL Server Express 2005
    2005-05-26 21:28:34  Wei-Meng Lee | O'Reilly Author [Reply | View]

    Hi Mark:
    By default, web parts uses the SQL 2005 Express provider for data storage. To use SQL Server 2000, you need to do some work:

    1. Use the aspnet_regsql.exe tool located in
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\ to prepare SQL Server 2000. Essentially it will add the aspnetdb database.

    2. In Web.config, add the following:

    <connectionStrings>
    <add name="myconnstring"
    connectionString="Data Source=your_server_name;Initial Catalog=aspnetdb;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    <system.web>
    ...
    ...

    <webParts>
    <personalization
    defaultProvider="SqlPersonalizationProvider">
    <providers>
    <add name="SqlPersonalizationProvider"
    type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
    connectionStringName="myconnstring"
    applicationName="/" />
    </providers>
    <authorization>
    <deny users="*" verbs="enterSharedScope" />
    <allow users="*" verbs="modifyState" />
    </authorization>
    </personalization>
    </webParts>
    </system.web>

    That should work fine for you.

    Hope it helps.
    BTW, Firefox doesn't seem to work too well with Web parts.

    regards,
    Wei-Meng
    • Apparently this demo requires SQL Server Express 2005
      2007-04-19 22:17:30  shhhh [Reply | View]

      when i use this code in web.config,it gives following error:
      Parser Error Message: The provider 'SqlPersonalizationProvider' specified for the defaultProvider does not exist in the providers collection.

      Source Error:


      Line 129:
      Line 130: <webParts>
      Line 131: <personalization defaultProvider="SqlPersonalizationProvider">
      Line 132: <providers>
      Line 133: <clear/>

    • Firefox and AJAX capabilities for WebParts
      2006-07-23 22:55:48  dipg [Reply | View]

      Found this usercontrol on www.smartjungle.com which provides a WebParts control to get AJAX capabilities and complete Firefox compatibility with no extra code.
      http://www.smartjungle.com
      Demos:
      http://www.smartjungle.com/Demos.aspx

    • Apparently this demo requires SQL Server Express 2005
      2006-01-21 14:08:59  thompson1022 [Reply | View]

      I have sql 2005 and was experiencing the same issue. This solution worked for me. Thank you very much.

      Jeff