locked
Help with DesignerView and Custom Control RRS feed

  • Question

  • User-1800386268 posted

    Hey guys, I´ve got a custom server control and it works like I want it to, However, in design view, it won´t work.

    I´ve got the following IDesigner:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.WebControls;
    using System.Resources;
    
    namespace CustomTools
    {
        class CustomButtonDesigner: ControlDesigner
        {
            public override string GetDesignTimeHtml()
            {
    
                
                string designTimeHtml = null;
              
    
                    designTimeHtml = "<div style=\"width:120px;height:40px;background-color: gray;color: white; font-weight: bold;text-align: center; border:2px solid white;\">"+ this.ID.ToString() +"</div>";
    
                  
              
    
                return designTimeHtml;
            }
    
            public override void Initialize(IComponent component)
            {
    
                if (!(component is Control) && !(component is INamingContainer))
                {
                    throw new ArgumentException("component must be a container control");
                }
    
    
                    base.Initialize(component);
                
                
    
                
            }
    
    
    
    
    
        }
    }
    

    AS you can see, I am simply loading a Div with a background color adn the name of the control. Nothing special.

    When I load te control in DesignView, if it´s by itself it loads properly, however, if I place it inside an ASP table or a Placeholder, it will give me the following error in design view it will make these not render in design view (again, it all works as expected on runtime)

    For example, I´ve got one instance of my control by itself, and below it, I have a Table with an instance of my control inside.

    as shown:

      <cc1:RDCButton ID="CustomButton1" runat="server" ButtonText="asdf" ControlLock="False" alarmString="0000000000" CssClass="buttonClass" Height="40px" Width="120px"></cc1:RDCButton>
            <br />
            <br />
            <asp:Table ID="Table1" runat="server">
                <asp:TableRow runat="server">
                    <asp:TableCell runat="server">
                        <cc1:RDCButton ID="CustomButton2" runat="server" ButtonText="asdf" ControlLock="False" alarmString="0000000000" CssClass="buttonClass" Height="40px" Width="120px"></cc1:RDCButton>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>

    On DesignTime, the CustomButton1 control, which is by itself, will render properly, while the CustomButton2, which is inside the table, will throw this error:

    Error Rendering Control - Table1

    An unhandled exception has ocurred.

    Object reference not set to an instance of an object.


    If I then place the CustomButton2 in a Placeholder, I will get:

    Error Rendering Control - PlaceHolder1

    An unhandled exception has ocurred.

    Object reference not set to an instance of an object.

    Any Ideas as to what may be causing this? How can i work around it?

    All help is appreciated!

    Wednesday, February 15, 2012 11:11 AM

All replies

  • User1838151321 posted

    Insert a Try Catch to debug what happens

    public override void Initialize(IComponent component)
            {
    Try
    {
                if (!(component is Control) && !(component is INamingContainer))
                {
                    throw new ArgumentException("component must be a container control");
                }
    
    
                    base.Initialize(component);
                
                }
    Catch(Exception ex)
    {
    Throw new Exception("Some error occurred!")
    }
                
            }
    Wednesday, February 15, 2012 3:50 PM
  • User-1800386268 posted

    Hmmm...

    I thought catching and rethrowing an exception was not good practice...?

    Wednesday, February 15, 2012 3:53 PM
  • User3866881 posted

    Hello Pizana:)

    I'm afraid in the design time,the parent componenets haven't been all initialized……So you cannot use them。So you get null exception。

    Thursday, February 16, 2012 8:35 PM
  • User-1800386268 posted

    Thanks for he reply Decker!

    But isnt that the purpose of the Designer template? To either provide this initialization OR to do something else, in this case, so that I don´t initilize my components, I just want to place a box with the name of the control. Easy as that. The weird part is where it works when the custom control is standalone, but when it´s inside an ASP.net table or an asp.net placeholder, it throws this error...

    any IDeas?

    Friday, February 17, 2012 7:07 AM
  • User1957418580 posted

    Is there a reason that you don't just override the "RenderContents" in the control itself?  Follow:

    http://msdn.microsoft.com/en-US/library/yhzc935f(v=vs.80).aspx

    I haven't looked deeply into this, but my guess is that the issue you are seeing is because the designer is trying to write out the table, but it doesn't have the information for the childcontrol yet.

    Monday, February 20, 2012 9:20 AM
  • User-1800386268 posted

    In the control itself I did override the RenderContents method.

    What I am talking about is in the ControlDesigner, so that I am able to render the control in design time.

    Like I said, the control by itself renders properly in designtime.

    However, when the control is inside an ASP.net Table, then I get the error described above...

    any insight on this?

    Thursday, April 12, 2012 11:39 AM
  • User-16411453 posted

    You don't have the structure needed for your control to work in both design view and runtime.

    Server Controls have a basic format to follow

    Render, OnInit, OnLoad, and Properties

    When you run the Server Control Wizard, it writes the basic format for you automatically, all you have to do is add a couple of more functions, and it will compile and work properly.

    Render - renders your control for output to design view or run, you can fork inside the render function for output to either.

    OnInit - You create your control in pure code, you can't create a string "<div></div>, but instead Dim panel_Div as Panel = new Panel

    OnLoad - just like page.load, after you create your html object, you now program them with values

    Properties - customize values for your control in design view, and they appear in both design view and runtime.

    So go back and rewrite your control again from scratch, and do it the right way.

    Saturday, April 21, 2012 7:36 PM
  • User1065003969 posted

    Look at this article:

    http://msdn.microsoft.com/en-us/library/aa479016.aspx

    If you are inheriting from a composite control, you can implement ICompositeControlDesignerAccessor and get a standard smart tag automatically applied to your control.

    Thursday, September 27, 2012 4:01 PM