Help with DesignerView and Custom Controlhttp://forums.asp.net/t/1769951.aspx/1?Help+with+DesignerView+and+Custom+ControlThu, 27 Sep 2012 20:01:17 -040017699514834056http://forums.asp.net/p/1769951/4834056.aspx/1?Help+with+DesignerView+and+Custom+ControlHelp with DesignerView and Custom Control <p>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.</p> <p></p> <p>I´ve got the following IDesigner:</p> <p></p> <pre class="prettyprint">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 = &quot;&lt;div style=\&quot;width:120px;height:40px;background-color: gray;color: white; font-weight: bold;text-align: center; border:2px solid white;\&quot;&gt;&quot;&#43; this.ID.ToString() &#43;&quot;&lt;/div&gt;&quot;; return designTimeHtml; } public override void Initialize(IComponent component) { if (!(component is Control) &amp;&amp; !(component is INamingContainer)) { throw new ArgumentException(&quot;component must be a container control&quot;); } base.Initialize(component); } } }</pre> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p>AS you can see, I am simply loading a Div with a background color adn the name of the control. Nothing special.</p> <p></p> <p>When I load te control in DesignView, if it&acute;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)</p> <p></p> <p>For example, I&acute;ve got one instance of my control by itself, and below it, I have a Table with an instance of my control inside.</p> <p></p> <p>as shown:</p> <p></p> <p></p> <pre class="prettyprint"> &lt;cc1:RDCButton ID="CustomButton1" runat="server" ButtonText="asdf" ControlLock="False" alarmString="0000000000" CssClass="buttonClass" Height="40px" Width="120px"&gt;&lt;/cc1:RDCButton&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Table ID="Table1" runat="server"&gt; &lt;asp:TableRow runat="server"&gt; &lt;asp:TableCell runat="server"&gt; &lt;cc1:RDCButton ID="CustomButton2" runat="server" ButtonText="asdf" ControlLock="False" alarmString="0000000000" CssClass="buttonClass" Height="40px" Width="120px"&gt;&lt;/cc1:RDCButton&gt; &lt;/asp:TableCell&gt; &lt;/asp:TableRow&gt; &lt;/asp:Table&gt;</pre> <p></p> <p></p> <p>On DesignTime, the CustomButton1 control, which is by itself, will render properly, while the CustomButton2, which is inside the table, will throw this error:</p> <p></p> <p>Error Rendering Control - Table1</p> <p>An unhandled exception has ocurred.</p> <p>Object reference not set to an instance of an object.<br> <br> <br> If I then place the CustomButton2 in a Placeholder, I will get:</p> <p></p> <p>Error Rendering Control - PlaceHolder1</p> <p>An unhandled exception has ocurred.</p> <p>Object reference not set to an instance of an object.</p> <p></p> <p></p> <p>Any Ideas as to what may be causing this? How can i work around it?</p> <p></p> <p>All help is appreciated!</p> <p></p> 2012-02-15T15:11:02-05:004834530http://forums.asp.net/p/1769951/4834530.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Insert a Try Catch to debug what happens</p> <pre class="prettyprint">public override void Initialize(IComponent component) { Try { if (!(component is Control) &amp;&amp; !(component is INamingContainer)) { throw new ArgumentException(&quot;component must be a container control&quot;); } base.Initialize(component); } Catch(Exception ex) { Throw new Exception(&quot;Some error occurred!&quot;) } }</pre> 2012-02-15T19:50:28-05:004834539http://forums.asp.net/p/1769951/4834539.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Hmmm...</p> <p></p> <p>I thought catching and rethrowing an exception was not good practice...?</p> 2012-02-15T19:53:25-05:004836816http://forums.asp.net/p/1769951/4836816.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Hello Pizana</p> <p>I'm afraid in the design timethe parent componenets haven't been all initializedSo you cannot use themSo you get null exception</p> 2012-02-17T00:35:49-05:004837498http://forums.asp.net/p/1769951/4837498.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Thanks for he reply Decker!</p> <p></p> <p>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...</p> <p></p> <p></p> <p>any IDeas?</p> 2012-02-17T11:07:00-05:004841270http://forums.asp.net/p/1769951/4841270.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Is there a reason that you don't just override the &quot;RenderContents&quot; in the control itself?&nbsp; Follow:</p> <p><a href="http://msdn.microsoft.com/en-US/library/yhzc935f(v=vs.80).aspx">http://msdn.microsoft.com/en-US/library/yhzc935f(v=vs.80).aspx</a></p> <p>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.</p> 2012-02-20T13:20:03-05:004929507http://forums.asp.net/p/1769951/4929507.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>In the control itself I did override the RenderContents method.</p> <p></p> <p>What I am talking about is in the ControlDesigner, so that I am able to render the control in design time.</p> <p></p> <p>Like I said, the control by itself renders properly in designtime.</p> <p></p> <p>However, when the control is inside an ASP.net Table, then I get the error described above...</p> <p></p> <p></p> <p>any insight on this?</p> <p></p> 2012-04-12T15:39:11-04:004944374http://forums.asp.net/p/1769951/4944374.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>You don't have the structure needed for your control to work in both design view and runtime.</p> <p>Server Controls have a basic format to follow</p> <p>Render, OnInit, OnLoad, and Properties</p> <p>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.</p> <p>Render - renders your control for output to design view or run, you can fork inside the render function for output to either.</p> <p>OnInit - You create your control in pure code, you can't create a string &quot;&lt;div&gt;&lt;/div&gt;, but instead Dim panel_Div as Panel = new Panel</p> <p>OnLoad - just like page.load, after you create your html object, you now program them with values</p> <p>Properties - customize values for your control in design view, and they appear in both design view and runtime.</p> <p>So go back and rewrite your control again from scratch, and do it the right way.</p> <p></p> <p></p> 2012-04-21T23:36:50-04:005161551http://forums.asp.net/p/1769951/5161551.aspx/1?Re+Help+with+DesignerView+and+Custom+ControlRe: Help with DesignerView and Custom Control <p>Look at this article:</p> <p>http://msdn.microsoft.com/en-us/library/aa479016.aspx</p> <p>If you are inheriting from a composite control, you can implement ICompositeControlDesignerAccessor and get a standard smart tag automatically applied to your control.</p> 2012-09-27T20:01:17-04:00