How To Add Design Time Support to Custom Controls

In this post i will show how to create custom control and also how to add design time support . When inheriting from the WebControl class, you must override the Render method to provide the desired output. The following is a code sample of LogoControl that contains a property for the LogoUrl and the CompanyName:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
using System.Drawing;
using System.ComponentModel;

namespace CustomControl
{
 [Designer("CustomControl.LogoControlDesigner, CustomControl")]
 [ToolboxData(@"<{0}:LogoControl runat=""server"" CompanyName="" "" LogoUrl="" "" />")]
 public class LogoControl : WebControl
 {
     public LogoControl()
     {
     }
     public string LogoUrl
     {
         get { return _logoUrl; }
         set { _logoUrl = value; }
     }
     private string _logoUrl;
     public string CompanyName
     {
         get { return _companyName; }
         set { _companyName = value; }
     }
     private string _companyName;
     protected override void Render(HtmlTextWriter writer)
     {
         writer.WriteFullBeginTag("div");
         writer.Write(@"<img src=""{0}"" /><br />", LogoUrl);
         writer.Write(CompanyName + "<br />");
         writer.WriteEndTag("div");
     }
 }
}
  • The ToolboxBitmapAttribute class is located in the System.Drawing namespace in the System.Drawing.dll assembly. The following code snippet shows the ToolboxBitmap being set to a .bmp file that is configured to be an embedded resource. (When you use the ToolBoxBitmapAttribute, the Attribute suffix is optional.)
  • You can also change the namespace prefix that is assigned by the Web page designer by assigning the TagPrefixAttribute to the assembly that contains your custom control. The following snippet shows the namespace prefix being changed to “mcl” for the controls in the Customcontrol project
[assembly: TagPrefix("LogoControl", "mcl")]
Now we add custom designer for our that is used to render the control when in design mode by adding a reference to the System.Design.dll assembly and creating a class that inherits from the ControlDesigner class. This can be beneficial when the normal rendering of the control may not be visible due to code that needs to run to populate specific properties. For example, you may want to provide a custom designer for the LogoControl that provides a default rendering when the LogoUrl property has not been set, as shown in the following code sample:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
using System.Drawing;
using System.ComponentModel;

namespace CustomControl
{
   class LogoControlDesigner : System.Web.UI.Design.ControlDesigner
   {
       private LogoControl _logoControl;
       public override string GetDesignTimeHtml()
       {
           if (_logoControl.LogoUrl.Trim().Length == 0)
           {
               return "<div id=\'mcl1\' "
               +
               "style=\'background-color:red;border-width:2px;\' >"
               +
               "<center>LogoControl</center><br />"
               +
               "<center>Please set LogoUrl property.</center><br />"
               + "</div>";
           }
           else
           {
               return base.GetDesignTimeHtml();
           }
       }
       public override void Initialize(IComponent component)
       {
           _logoControl = (LogoControl)component;
           base.Initialize(component);
           return;
       }
   }
}

No comments:

Post a Comment