text: Feb 15, 2012 03:11 PM|LINK
  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;"+ 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</cc1:RDCButton> <br /> <br /> <asp:Table <asp:TableRow <asp:TableCell <cc1:RDCButton</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!
  customcontrol error rendering control
  Feb 15, 2012 07:50 PM|LINK
  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!") } }
  All-Star
  118619 Points
  Feb 17, 2012 11:07 AM|LINK
  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?
  Contributor
  3318 Points
  Microsoft
  Feb 20, 2012 01:20 PM|LINK
  Is there a reason that you don't just override the "RenderContents" in the control itself? Follow:
  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.
  Apr 12, 2012 03:39 PM|LINK
  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?
  Apr 21, 2012 11:36 PM|LINK
  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.
  Sep 27, 2012 08:01 PM|LINK
  Look at this article:
  If you are inheriting from a composite control, you can implement ICompositeControlDesignerAccessor and get a standard smart tag automatically applied to your control.
  customcontrol error rendering control
  8 replies
  Last post Sep 27, 2012 08:01 PM by jasoncmcg