Enable Customer Reviews
Create a custom control that adds reviewing and rating functionality to e-commerce sites.
by Fabio Claudio Ferracchiati
August 2003 Issue
Technology Toolbox: VB.NET, ASP.NET
Popular e-commerce sites, such as Amazon and eBay, allow users to review and comment on products available for sale on the site. This functionality lets customers consider impartial comments when they choose among products, and helps sellers understand customer preferences. I'll show you how to implement your own reviewing functionality by using a powerful technique you can reuse on any page—the ASP.NET custom control.
An ASP.NET custom control is an assembly that contains one or more classes that derive from the WebControl class in the System.Web.UI.WebControls namespace. Creating an ASP.NET custom control is similar to building a WinForms control: You can use the overridable Render method to provide a user interface and to display graphics. You can build the HTML representing your control's output in the Render method's body and use the HtmlTextWriter method's parameter to write the HTML into the ASP.NET page. You can also specify new properties and use VB.NET language attributes to specify default values, descriptions, VS.NET Properties window categories, and much more.
When a page containing the custom control loads, the control receives many sequential events you can trap to customize the component's behavior (see Table 1). You usually use the Init event to initialize the control's member variables and the Render event to write the HTML code into the ASP.NET page. You can implement IPostBackDataHandler and IPostBackEventHandler interfaces for your component class to add extra events, such as LoadPostData, which retrieves incoming form data so you can manage it before the control uses it.
Now I'll show you how to build the ReviewerCtrl custom control to add review functionality to your pages ( download the source code here). ReviewerCtrl comprises three classes. The AverageCtrl class, which derives from the WebControl class, displays an image representing the product's ratings average. The ListCtrl class, which also derives from the WebControl class, displays an HTML table containing either the last user comment or a list of all comments related to the specified product identifier. The Vote class, which derives from the Object class, provides the InsertReview method, which allows you to associate a new review to the related product.
All three classes have two common properties: The ProductID property specifies the product identifier you use to associate ratings and reviews to the product, and the XMLPath property specifies the path of the XML files where you store ratings and reviews. You could use a database to store them, but XML—in conjunction with ADO.NET classes—is the best solution when you have a small amount of information to manage, because you don't waste resources by connecting to a database, and you're ready to share your data through Web services.
Change the Tagname Value
The <cc1:tagname> tag is used by default when you insert the ReviewerCtrl component into an ASP.NET Web form. You can change this value by adding the TagPrefix Attribute class to specify the component namespace and the new prefix:
<Assembly: TagPrefix("ReviewerCtrl", _
"VSM")>
You can also associate a new icon to a custom control by adding a 16-by-16 bitmap that has the same control class name and the Embedded Resource property. The control uses the new bitmap instead of the default one when you add the component to VS.NET's toolbox.
Back to top
|