Wrapping Standard Controls for Use in Web Parts

 

Wrapping Standard Controls for Use in Web Parts

Web Part controls can be a good solution, especially when you are creating a control from scratch. However, you might often have an existing control that would make a great Web Part. What problems might you face when using your existing user controls or custom controls?

Looking back at the examples in Chapter 4, notice that all of the native controls not derived from the WebPart class dropped into a WebPartZone control have "Untitled" as their title; of course, customizing the icon is not possible. The same is also true of adding user controls as Web Parts.

So, do you have to give up custom titles or icons to get the advantages of user controls? Thankfully, the answer is no. Recall the code used in Chapter 4 to programmatically add a TextBox control as a Web Part.

GenericWebPart gwp = this.WebPartManager1.CreateWebPart(tb); this.WebPartManager1.AddWebPart(gwp, this.WebPartZone1, 0); 

The GenericWebPart class acts as a wrapper for the class when you create a Web Part. If you wanted to, you could set properties of the GenericWebPart control in the Page_Init event handler of the user control, as shown here.

void Page_Init(object sender,EventArgs e) {     GenericWebPart gwp = Parent as GenericWebPart;     if ( gwp != null )     {         gwp.Title="New User Control/Web Part Title";     } } 

Another alternative is to implement the IWebPart interface in a user control. The IWebPart interface consists of several properties. These properties are the values that you would probably want to change, such as Title and TitleIconImageUrl.

To demonstrate how implementing the IWebPart interface works in a user control, I created a copy of DisplayBlogEntryUC.aspx, naming it DisplayBlogEntryUCasWP.ascx. In the class declaration, I added the IWebPart interface after UserControl in the class declaration, to indicate that the class implements the IWebPart interface, as shown here.

public partial class DisplayBlogEntryUCasWP :     System.Web.UI.UserControl,IWebPart 

Visual Studio 2005 includes several new refactoring tools. One of the most useful of these tools offers the ability to right-click an interface name in code and select Implement Interface from the resulting context menu. Doing this creates default implementations for all members of the interface. These default implementations simply throw an Exception object, with a message that the method or operation is not implemented. I did this and modified the generated code to provide the correct functionality. Listing 6-14 shows the resulting implementation of the IWebPart interface in DisplayBlogEntryUCasWP.ascx.cs.

Listing 6-14: The Implementation of the IWebPart Interface in DisplayBlogEntryUCasWP.ascx.cs

image from book
#region IWebPart Members private string catalogIconImageUrl; private string description; private string subTitle; private string title; private string titleIconImageUrl; private string titleUrl; public string CatalogIconImageUrl {     get     {         return catalogIconImageUrl;     }     set     {         catalogIconImageUrl=value;     } } public string Description {     get     {         return description;     }     set     {         description=value;     } } public string Subtitle {     get { return subTitle; } } public string Title {     get     {         return title;     }     set     {         title=value;     } } public string TitleIconImageUrl {     get     {         return titleIconImageUrl;     }     set     {         titleIconImageUrl = value;     } } public string TitleUrl {     get     {         return titleUrl;     }     set     {         titleUrl=value;     } } #endregion 
image from book

Visual Studio conveniently wraps the implementation of the interface inside a set of #region and #endregion directives. The constructor and Page_Load event handler are also different, as shown here.

public DisplayBlogEntryUCasWP() {     catalogIconImageUrl=string.Empty;     description = string.Empty;     subTitle = string.Empty;     title = "Bike Blog";     titleIconImageUrl = string.Empty;     titleUrl = string.Empty; } protected void Page_Load(object sender, EventArgs e) {     // Should really get most recent entry...     this.BlogEntryID = 7; } 

The constructor for DisplayBlogEntryUCasWP sets some of the supporting private data members for the IWebPart properties. Finally, the Page_Load event handler sets the BlogEntryID property of the user control. In the real world, rather than hard coding the BlogEntryID property, the code would find the most recent blog entry and display that (we would use data retrieval code similar to the previous examples in the chapter).

Like I did with TestSimpleWebPart.aspx, I set up a new page in the CustomControls Web site, named TestUCasWP.aspx. I added the same DropDownList control and Web Part related controls, and I set the schemes of all the Web Part related controls to Professional.

The Page_Load event handler for TestUCasWP.aspx contains the code required to load the user control, assuming there is not already a Web Part on the page. The code to do that is shown here.

protected void Page_Load(object sender, EventArgs e) {     if (this.WebPartManager1.WebParts.Count == 0)     {         Control uc = this.LoadControl("~/DisplayBlogEntryUCasWP.ascx");         uc.ID = "UCasWP1";         GenericWebPart gwp = WebPartManager1.CreateWebPart(uc);         this.WebPartManager1.AddWebPart(gwp, this.WebPartZone1, 0);     } } 

The LoadControl method of the Page class takes the URL of the user control and loads an instance of the control. The LoadControl method is often used to dynamically load a user control. The code that follows is identical to the code in TestSimpleWebPart.aspx that dynamically loaded controls as Web Parts.

When run, TestUCasWP.aspx looks like Figure 6-25.

image from book
Figure 6-25: TestUCasWP.aspx in Browse mode

The look of the blog entry shown in Figure 6-25 is very different from the version shown in Figure 6-7. Without the style sheet used in Figure 6-7, the blog entry takes on the appearance specified by the Auto Format scheme. Also noteworthy is that, rather than "Untitled," the user control shown in Figure 6-25 is titled "Bike Blog."

When you switch to Design mode, you can drag the user control, which is masquerading as a Web Part, from one Web Part zone to another, as shown in Figure 6-26.

image from book
Figure 6-26: TestUCasWP.aspx in Design mode, dragging a user control based Web Part

 


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