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.
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?
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!")
}
}
Marco Cavallo
WebMaster & Programmer
Il mio sito in continua evoluzione...
Vieni a trovarmi!
http://www.artcava.net/
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...
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.
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.
If you are inheriting from a composite control, you can implement ICompositeControlDesignerAccessor and get a standard smart tag automatically applied to your control.
Pizana
Member
56 Points
71 Posts
Help with DesignerView and Custom Control
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; 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!
customcontrol error rendering control
ArtCava
Member
18 Points
9 Posts
Re: Help with DesignerView and Custom 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!") } }WebMaster & Programmer
Il mio sito in continua evoluzione...
Vieni a trovarmi!
http://www.artcava.net/
Pizana
Member
56 Points
71 Posts
Re: Help with DesignerView and Custom Control
Feb 15, 2012 07:53 PM|LINK
Hmmm...
I thought catching and rethrowing an exception was not good practice...?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with DesignerView and Custom Control
Feb 17, 2012 12:35 AM|LINK
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。
Pizana
Member
56 Points
71 Posts
Re: Help with DesignerView and Custom Control
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?
cts-mgraham
Contributor
3318 Points
642 Posts
Microsoft
Re: Help with DesignerView and Custom Control
Feb 20, 2012 01:20 PM|LINK
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.
Pizana
Member
56 Points
71 Posts
Re: Help with DesignerView and Custom Control
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?
jkirkerx
Contributor
3750 Points
873 Posts
Re: Help with DesignerView and Custom Control
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.
jasoncmcg
Member
7 Points
6 Posts
Re: Help with DesignerView and Custom Control
Sep 27, 2012 08:01 PM|LINK
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.
customcontrol error rendering control