Custom Web Parts

 

Recall from Chapter 4 that Web Parts are a new feature of ASP.NET 2.0. Standard server controls can be added to Web Part zones, but you can also add user controls; or perhaps even better, a developer can create custom Web Parts to take fuller advantage of the Web Parts environment. Custom Web Parts all derive from the System.Web.UI.WebParts.WebPart class.

Deriving from the System.Web.UI.WebParts.WebPart class allows you to control the appearance and behavior of a control displayed in a Web Part enabled page. First, let's look at a very simple example Web Part, shown in Listing 6-13.

Listing 6-13: SimpleWebPart.cs

image from book
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Summary description for SimpleWebPart /// </summary> public class SimpleWebPart : System.Web.UI.WebControls.WebParts.WebPart {     private string text;     private bool allowClose;     public string Text     {         get         {             return text;         }         set         {             text = value;         }     }     public override bool AllowClose     {         get         {             return false;         }         set         {             // Ignore...         }     }     public SimpleWebPart()     {         this.Title = "Simple Web Part";         text = "This is a Simple Web Part.";     }     protected override void RenderContents(HtmlTextWriter writer)     {         writer.Write(Text);     } } 
image from book

I started from a new Class file in the CustomControlsLib project site, which I named SimpleWebPart.cs.

Note 

Much of this chapter has covered creating user controls and the power possible with those controls. The designer support for user controls in Visual Studio 2005 is quite good, and the ability to create the markup declaratively rather than programmatically is hard to beat. However, there is no designer support for Web Part derived controls in Visual Studio, and any user interface created for a WebPart-derived class must be created programmatically.

I changed the declaration for the class so that it would inherit from the System.Web.UI.WebParts. WebPart class. In addition to the Text property used in previous examples in this chapter, I also provided an override for the AllowClose property. By always returning false, I prevented the Web Part from being closed. There is no particular reason in this case for not allowing the Web Part to be closed, but it demonstrates some of the power of creating your own Web Part. The constructor for the Web Part sets some properties, including Title, which identifies the Web Part when displayed. Other properties can be modified as well, including TitleIconImageUrl, which customizes the title icon image. Finally, I provided an override for the RenderContents method that simply writes out the Text property.

Tip 

If you don't have SQL Server 2005 Express Edition installed, you might receive a timeout error message the first time you run the application. If you are running the full version of SQL Server 2005 rather than SQL Express, you can resolve the problem the same way we resolved it in Chapter 4: Simply copy the following lines from the Web.config file from the WebParts Web site to the CustomControls Web site.

<remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="data    source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb"    providerName="System.Data.SqlClient"/> 

To test this simple Web Part, I created a new Web Form in the CustomControls Web site named TestSimpleWebPart.aspx. To that new page, I added several components:

  • A WebPartManager control, a non-visual control that must be added to enable the page to handle Web Parts.

  • A DropDownList control, to allow the user to control the mode of the page. I added three items to the control ("Browse," "Edit," and "Design"), and then I set the AutoPostback property to true, so that the page is notified of changes immediately.

  • Two WebPartZone controls.

  • A CatalogZone control.

  • A PageCatalogZone control, which must be added to the CatalogZone control.

For all of the controls with a Tasks menu (accessed from the smart tag), I selected the Professional scheme from the Auto Format dialog box. When completed, the TestSimpleWebPart.aspx page looked like Figure 6-23 in Design view.

image from book
Figure 6-23: TestSimpleWebPart.aspx in Design view

In Browse mode, the screen appeared as shown in Figure 6-24.

image from book
Figure 6-24: TestSimpleWebPart.aspx in Browse mode

In addition to the code that handles the change of display mode (which was covered in Chapter 4), TestSimpleWebPart.aspx also has a bit of code in the Page_Load event handler to ensure that there are always at least three instances of the SimpleWebPart control on the page. The code from the Page_Load event handler is shown here.

protected void Page_Load(object sender, EventArgs e) {      if (this.IsPostBack==false)     {         // Ensure there are at least three instances of the Web Part         while (WebPartManager1.WebParts.Count < 3)         {             WebPart wp = new SimpleWebPart();             WebPartManager1.AddWebPart(wp, this.WebPartZone1,                 WebPartManager1.WebParts.Count);         }     } } 

If fewer than three of our custom Web Parts are registered, the code creates a new instance of the SimpleWebPart class and adds the Web Part to the WebPartZone1 control.

 


Programming Microsoft Web Forms
Programming Microsoft Web Forms (Pro Developer)
ISBN: 0735621799
EAN: 2147483647
Year: 2005
Pages: 70
Authors: Douglas J. Reilly
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net