Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Work Web Part Magic Inside of ASP.NET  : Page 2

SharePoint users have known how useful Web parts are for a long time, but it wasn't until recently that every .NET developer had access to the ease and grace of Web parts using ASP.NET 2.0. Find out how you can use these handy content containers to create Web sites that put routine content sharing capabilities into the hands of your end users.


advertisement
Creating a Web Site to Host your Web Part
Before creating your Web part it is helpful to have a Website that you can add it to. In this section I'll guide you through the step-by-step process of creating a Web page that you can use to host your Web parts and test that they are working. If you don't have an IIS virtual server setup, you'll need to set one up. Once you have a Website to host your project, you're ready to start:
  1. Open Visual Studio.
  2. Select Create Web Site… from the Start Page.
  3. From the Language drop down, select the language for your project. In this example I'm using C#, so if you want to follow along select C#.
  4. Select ASP.NET Web Site from the templates (see Figure 1).
  5. In the location box, enter the location for your Web server. (The figures in this article will reflect http://localhost:83/WebParts.)


    Figure 1. Fresh Part Start: In this dialog you take the first step in creating the new Website for your Web part pages.
     
    Figure 2. Debug?: You'll be given the option to create a web.config with debugging turned on.

  6. Click OK.
  7. Click the Design tab to enter design mode of the default.aspx page.
  8. In the toolbox window (select View —> Toolbox if it is not already displayed) expand the Web parts section and drag the WebPartManager to the page.
  9. From the toolbox in the WebParts group, select WebPartZone and drag it into the page.
  10. Press F5 to run the Website.
  11. Answer 'Yes' when prompted to create a web.config and enable debugging (see Figure 2). (Note: This process will cause Visual Studio, IIS, and ASP.NET to complete a series of steps that require your server to be configured correctly. If for any reason your server isn't configured correctly you'll see an error page that will advise you how to resolve the configuration options.)
  12. When your blank page comes up, close Internet Explorer; you're about to create your new Web part.

Create Your Web Part

  1. Click the solution in Solution Explorer.
  2. Right-click the solution, select Add, and finally select New Project.
  3. Expand the Visual C# item in the Project types pane.
  4. Click the Windows item under Visual C# in the Project types.
  5. Click the Web Control Library option in the templates section.
  6. In the name textbox, enter a name for the Web control. In this example, we'll call the library SampleWebParts.
  7. When the WebCustomControl1.cs file opens add a new using statement: "using System.Web.UI.WebControls.WebParts."
  8. Change the base class for WebCustomControl1 from WebControl to WebPart.
  9. In the solution explorer pane, right-click the WebCustomControl1.cs file and give it the name of TextWebPart.
  10. When prompted to rename all references, click the Yes button.
  11. Update the class level ToolboxData attribute changing both (start and end) WebCustomControl1 references to TextWebPart.
  12. Above the Text property add the following attributes:
    • [WebBrowsable]
    • [Personalizable]
    • [WebDisplayName("Text to Display")]
    • [WebDescription("The text the Web part will display")]
  13. Build the solution (Ctrl-Shift-B) to make sure that your Web part compiles.
Add the Web Part to the Page
Figure 3. Thou Art Web Part: A bare bones Web part is displayed on the ASP.NET Web part page created in this article.
Adding the Web part page is a two-part process. The first part of the process adds the Web part to the declarative catalog; the second part adds the Web part to the page. Both steps are covered in the following process:
  1. In Design view on the default.aspx page, drag the TextWebPart from the SampleWebPartsComponents into the Web part zone.
  2. Switch to source view on the default.aspx page. In the SampleWebParts:TextWebPart tag add a Title attribute with something in it such as "Web Part Title") and a Text attribute with something in it such as "This is a test."
  3. Press F5 to run your Web application and Web part.
  4. You should see your Web part with the title and text you specified. Figure 3 shows the result of the page when put together.



Web Parts are not inherently complicated but are inherently powerful. In this article you learned what ASP.NET Web parts are, how they relate to SharePoint Web parts, how they work, and created a functioning Web part.



Robert Bogue, MCSE (NT4/W2K), MCSA:Security, A+, Network+, Server+, I-Net+, IT Project+, E-Biz+, CDIA+ has contributed to more than 100 book projects and numerous other publishing projects. He was recently honored to become a Microsoft MVP for Microsoft Commerce Server and before that Microsoft Windows Servers-Networking. Robert blogs at http://www.thorprojects.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap