locked
TypeConverter - object reference assignment RRS feed

  • Question

  • User-150770710 posted

    Hi
    I have write a simple webcontrol, which contains properties as below

    public class MyClass : WebControl
    {
        public Button myButton { get { } set { } }
    }

    I would like to add design-time support for this control to ease my developer. Once myClass is dropped on web form, I want developer to able to go to property designer, navigate to "myButton" property row, click the drop down and select one of the button exist in current web form. 

    As now, the aspx code generated is 

    <cc1: myClass ID="xxx" runat="server" myButton = "OneOfmyButton1"/>

    When run the app, I will hit error. I would like to ask for sample how to generate code in designer.cs so that I can have

    this.xxx.myButton = this.OneOfmyButton1;

    I search from web, but only found expandableObject, InstanceDescriptor ... which I am not sure it is useful for my case.

    Please advice. thank you

    Friday, May 4, 2012 2:17 AM

Answers

  • User3866881 posted

    Hello:)

    You should override the ControlIdConvertor to deal with the problem by checking whether the current is Id or not:

     public class ServerControl1 : WebControl
        {
            private class MyButtonConverter : ControlIDConverter { protected override bool FilterControl(Control control) { return control is Button; } }
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            [TypeConverter(typeof(MyButtonConverter))]
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null) ? "[" + this.ID + "]" : s);
                }
    
                set
                {
                    ViewState["Text"] = value;
                }
            }
    
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 5, 2012 10:56 PM