We have a subclassed WebControl which is decorated with the Designer attribute and displays an image in GetDesignTimeHtml. It is built using VS 2008 Sp1. Under VS 2005, when placed in the toolbox, and dragged into an aspx page in design mode, it behaves just fine. However, in both VS 2008 Sp1 and VS 2010 Sp1, the image cannot be resized in design mode. I have recreated the problem using the following code:
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.ComponentModel.Design;
using System.Collections;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI.Design;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;
namespace Samples.AspNet.CS.Controls
{
[
Designer(typeof(WelcomeLabelDesigner)),
DefaultPropertyAttribute("ID"),
ToolboxBitmap(typeof(WelcomeLabel), "Samples.AspNet.CS.Controls.WecomeLabel"),
ToolboxData("<{0}:WelcomeLabel runat="server" Height= 256px Width= 256px > </{0}:WelcomeLabel>")
]
public class WelcomeLabel : WebControl
{
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("The welcome message text."),
Localizable(true)
]
public virtual string Text
{
get
{
string s = (string)ViewState["Text"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("Testing how the App_Code directory works.<br>");
writer.WriteEncodedText(Text);
if (Context != null)
{
string s = Context.User.Identity.Name;
if (s != null && s != String.Empty)
{
string[] split = s.Split( \ );
int n = split.Length - 1;
if (split[n] != String.Empty)
{
writer.Write(", ");
writer.Write(split[n]);
}
}
}
writer.Write("!");
}
}
internal class WelcomeLabelDesigner : ControlDesigner
{
protected WelcomeLabel parentControl;
public override void Initialize(IComponent component)
{
base.Initialize(component);
if (component is WelcomeLabel)
{
parentControl = (WelcomeLabel)component;
}
}
public override string GetDesignTimeHtml()
{
try
{
StringBuilder sb = new StringBuilder();
String url = parentControl.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Samples.AspNet.CS.Controls.resources.images.WelcomeLabel.bmp");
sb.Append("<img alt="background image" src="" + url + "" height="" + parentControl.Height + "px" width="" + parentControl.Width + "px"/>");
return sb.ToString();
} catch (Exception ex)
{
// Display the error in VS.net, in Design view
return String.Concat("<h3>Error</h3>Stack Trace:<br>", ex.StackTrace + " " + ex.Message + " " + ex.InnerException);
}
}
}
}