locked
problem rendering a server control which has a property that takes value from an enumaration in "design time" RRS feed

  • Question

  • User-1301850010 posted

    Dear friend,

    I have a simple server-side control that is developed like this :

        
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:Button runat=server></{0}:Button>")]
        public class Button : LinkButton 
        {        
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue(ButtonImageTypes.Save)]
            [Localizable(true)]
            public ButtonImageTypes ImageType {get;set;}

            }
        }
        public enum ButtonImageTypes
        {
            Save,
            Add,
            Check,
            Remove
        }


        
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:Button runat=server></{0}:Button>")]
        public class Button : LinkButton 
        {        
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue(ButtonImageTypes.Save)]
            [Localizable(true)]
            public ButtonImageTypes ImageType {get;set;}
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue(false)]
            [Localizable(true)]
            public bool DisablePostback{ get; set; }
            protected override void RenderContents(HtmlTextWriter output)
            {
                string css="Button";
                css += " " + getIconCss(ImageType);
                if (!this.Enabled) css += " disabled";
                output.Write(@"<div class='" + css + "'><div class='" + getIconCss(ImageType) + "'></div>" + Text + "</div>");
            }
            protected override void OnPreRender(EventArgs e)
            {
                if (DisablePostback)
                {
                    this.Attributes["href"] = "javascript:";
                    base.OnPreRender(e);
                }
            }
            private string getIconCss(ButtonImageTypes type)
            {
                switch (type)
                {
                    case ButtonImageTypes.Save:
                        return "save";
                    case ButtonImageTypes.Add:
                        return "add";
                    case ButtonImageTypes.Check:
                        return "check";
                    case ButtonImageTypes.Remove:
                        return "remove";
                    default:
                        return "";
                }
            }
        }
        public enum ButtonImageTypes
        {
            Save,
            Add,
            Check,
            Remove
        }b

    This control works fine in run-time and then it means it has no bug. Additionally, In design time, it works well in default mode and rendered correctly. But if I change the ImageType property, it fails (just in design-time). how can I fix it? 

    the sample design-time code is:

    <SPS:Button runat="server" ID="btn" 

                onclick="btnAddBaseEntity_Click" Width="120px" ImageType="Remove">Remove Item</SPS:Button>


    anyone can help?

    Tuesday, August 11, 2009 1:16 AM

Answers

  • User-1119887562 posted

    Have you tried setting the property using the Enumeration Name? 

    <SPS:Button runat="server" ID="btn" onclick="btnAddBaseEntity_Click" Width="120px" ImageType="ButtonImageTypes.Remove">Remove Item</SPS:Button>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2009 1:25 PM

All replies

  • User863160722 posted

    It sounds like the designer doesn't like automatic properties. Have you tried switching to a viewstate-backed property?

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue(ButtonImageTypes.Save)]
    [Localizable(true)]
    public ButtonImageTypes ImageType
    {
        get
        {
            return (ButtonImageTypes?)ViewState["ImageType"] ?? ButtonImageTypes.Save;
        }
        set
        {
            ViewState["ImageType"] = value;
        }
    }
    Tuesday, August 11, 2009 2:16 PM
  • User-1301850010 posted

     Dear Richard,

    Thanks for your fast reply. I did your proposed changes but the same error was happend (error message is: error rendering control. an unhandled exception has occurred. ButtonImageTypes could not be set on property 'Check'). The misery is that this error happens for each kind of user defined property!!!(String,Integer,...)... I think the source of error should be from somewhere else (Visual studio bug or forgetting to set an attribute).

    I also should mention that I tried the following solutions but the output was same :

    1- using common properties instead of automatic properties

    2-removing all the attributes

    3-trying to bgring the VS to debug mode to see what happened on "set" property. but none of "set" properties were fired.(just my breakpoints on "get" were captered)...I think this shows that the problem is happening at a stage before calling the "set" property.

    thanks

    P.S.

    Also it seems a VS 2008 SP1 bug.(check this : http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=478158 ) but I couldn't find a way to resolve it there! any help?

     

    Wednesday, August 12, 2009 12:58 AM
  • User-1119887562 posted

    Have you tried setting the property using the Enumeration Name? 

    <SPS:Button runat="server" ID="btn" onclick="btnAddBaseEntity_Click" Width="120px" ImageType="ButtonImageTypes.Remove">Remove Item</SPS:Button>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2009 1:25 PM