Web Custom Control Question about Inheriting from a Panel
-
My code is shown below. My problem is that when I uses this code it displays just fine while running the webapp but when in design mode I see nothing other than a blank panel? I've been searching around google and cp and can't find any help so I have resorted to asking :) Any help would be appreciated.
Public Class MyBar Inherits System.Web.UI.WebControls.Panel Protected Overrides Sub OnInit(ByVal e As System.EventArgs) EnsureChildControls() End Sub Dim _lbl As New Label Protected Overrides Sub CreateChildControls() _lbl.Text = "Hello World!" MyBase.Controls.Add(_lbl) End Sub Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) MyBase.Render(output) End Sub End Class
-Richard -
My code is shown below. My problem is that when I uses this code it displays just fine while running the webapp but when in design mode I see nothing other than a blank panel? I've been searching around google and cp and can't find any help so I have resorted to asking :) Any help would be appreciated.
Public Class MyBar Inherits System.Web.UI.WebControls.Panel Protected Overrides Sub OnInit(ByVal e As System.EventArgs) EnsureChildControls() End Sub Dim _lbl As New Label Protected Overrides Sub CreateChildControls() _lbl.Text = "Hello World!" MyBase.Controls.Add(_lbl) End Sub Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) MyBase.Render(output) End Sub End Class
-RichardWould this work for you: explicitly assign the PanelDesigner as your MyBar class' designer through the
DesignerAttribute
attribute?<Designer("System.Web.UI.Design.WebControls.PanelDesigner")> _
Public Class MyBar
Inherits System.Web.UI.WebControls.Panel
. . .
End Class -
Would this work for you: explicitly assign the PanelDesigner as your MyBar class' designer through the
DesignerAttribute
attribute?<Designer("System.Web.UI.Design.WebControls.PanelDesigner")> _
Public Class MyBar
Inherits System.Web.UI.WebControls.Panel
. . .
End ClassNope but I did find that if I specify the designer as System.Web.UI.Design.ControlDesigner the controls all show up the way they are suppose to, however I loose the ability to drag controls into my control. I found out that I could change my designer to ReadWriteControlDesigner but that makes it not draw the contorl correctly again... :( Learning new things is fun but aggravating at the same time. -Richard
-
Nope but I did find that if I specify the designer as System.Web.UI.Design.ControlDesigner the controls all show up the way they are suppose to, however I loose the ability to drag controls into my control. I found out that I could change my designer to ReadWriteControlDesigner but that makes it not draw the contorl correctly again... :( Learning new things is fun but aggravating at the same time. -Richard
Hi Richard, As you superclass the Panel control, so if you use a designer for the control you either use the
ReadWriteControlDesigner
or a custom designer which inherits from theReadWriteControlDesigner
likePanelDesigner
. At design time, you can drag/drop controls from the Toolbox onto your custom control, however, the control is not rendered properly in the design window because theGetDesignTimeHTML
method is not called. If you use theControlDesigner
instead you can see the custom control displayed correctly as theGetDesignTimeHTML
method is called, but you cannot drag/drop child controls any more. So there two things come to mind: + You don't programmatically add the label control in code, instead you can declare it in the ToolboxData attribute:[ToolboxData("<{0}:MyBar runat=server><asp:Label runat=server>Hello World</asp:Label></{0}:MyBar>")]
public class MyBar : System.Web.UI.WebControls.Panel
{
...
}The downside of this way is clear when you want to add many child controls in your custom container control by default. + You may think of developing a template control.