Here is an example on a webcontrol with a designer. This designer should show
a dropdownlist with the item "design-time item".
using System;
using System.ComponentModel;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
namespace MyCompany.MyControls
{
[ToolboxData("<{0}:MyWebControl runat=server></{0}:MyWebControl>"),
Designer("MyCompany.MyControls.MyWebControlDesigner, MyWebControl")]
public class MyWebControl : WebControl
{
....
}
public class MyWebControlDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
MyWebControl ctrl = (MyWebControl)Component;
// create the html-writer
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
// add a control that you want to show at design-time
DropDownList designTimeDropDownList = new DropDownList();
designTimeDropDownList.Items.Add("design-time item");
// get design-time styles and formatting from your control
MyWebControl ctrl = (MyWebControl)Component;
designTimeDropDownList.CssClass = ctrl.CssClass;
// let the design-time control render itself on the html-writer
designTimeDropDownList.RenderControl(tw);
// return the html-code from the html-writer
return sw.ToString();
}
}
}
Also add the following code to the AssemblyInfo.cs-file to get a nice
tag-prefix (<abc:MyWebControl runat="server" />)
using System.Web.UI;
[assembly: TagPrefix("MyCompany.MyControls", "abc")]
Kurt Yokum said:
Could use drop in a code sample that includes this Designer syntax? I am not
familiar with where this would go in the code.
Thanks,
Kurt
Kjetil Nordahl said:
Ok. I think I found the problem. It works now, anyway!
The problem was in the [Designer()] on the control-class.
the string-parameter to Designer should be:
[Designer("namespace-name.designerclass-name, assembly-name")]