使用Designer属性修饰的WebControl无法在Visual Studio 2008/2010设计器中调整大小

时间:2012-01-13 19:08:11

标签: c# asp.net custom-controls

我们有一个子类WebControl,它使用Designer属性进行修饰,并在GetDesignTimeHtml中显示一个图像。它是使用VS 2008 Sp1构建的。在VS 2005下,当放置在工具箱中,并在设计模式下拖入aspx页面时,它表现得很好。但是,在VS 2008 Sp1和VS 2010 Sp1中,图像无法在设计模式下调整大小。我使用以下代码重新创建了该问题:

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);    
            }    
        }    
    }    
}    

1 个答案:

答案 0 :(得分:0)

似乎返回的img没有严格解释为html。请注意,在GetDesignTimeHtml中返回以下内容确实有效:

sb.Append("<img alt=\"background image\" src=\"" + url + "\"  Height=\"" + parentControl.Height + "\" Width=\"" + parentControl.Width + "\"/>");