ContentPlaceHolder

Aquila
11-20-2006, 06:30 AM
Is there a way to use either the ContentPlaceHolder or some similar control in a customer web control? or do I just need to continue to make seperate "ControlStart" and "ControlEnd" controls?

Aquila
11-21-2006, 08:47 AM
Let me try and rephrase....

I want to create a control that I can do something like

<customthingy:foo id="bar" runat="server"><asp:label id="lblBlah" runat="server" /></customthingy:foo>

and the label will be put in my content section of the control I make... maybe by using a asp:placeholder or something... I'm just not finding much documentation on this. Anyone?

shaul_ahuva
11-21-2006, 08:48 AM
I assume you want a control you can drop content into? Why not make a base control supporting this:

<System.ComponentModel.Designer(GetType(System.Web.UI.Design.ReadWriteC ontrolDesigner), _
System.Web.UI.ParseChildren(False), _
System.Web.UI.PersistChildren(True)> _
Public Class EditableControl
Inherits System.Web.UI.Control

Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
Me.EnsureChildControls()

Me.Controls(PlaceHolder index).Controls.Add(DirectCast(obj, System.Web.UI.Control))
End Sub

Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()

'Add "header" controls
Me.Controls.Add(New System.Web.UI.WebControls.PlaceHolder())
'Add "footer" controls
End Sub
End Class

If you want to see the header and footer controls at design-time you'll need to create a custom designer, the design of which depends on the version of the framework you're using (.NET 2.0 has MUCH better support for read-only and read/write regions). If you just want to add controls in the markup, you can just apply the ParseChildren and PersistChildren attributes as noted above.

Aquila
11-21-2006, 09:11 AM
I'm using 2.0, and I've never written a control in this way I've always done .ascx files, I feel like a dunce for asking but could you show me an example of how to use a control like you've written above?

will this allow me to do the <customthingy:foo id="bar" runat="server"><asp:label id="lblBlah" runat="server" /></customthingy:foo> usage?

shaul_ahuva
11-21-2006, 09:56 AM
The code I posted is a very basic server control (like the TextBox, GridView, or any other control in the toolbox). It sounds like you've only created user controls. The primary difference between the two is that user controls use ascx files to add controls at runtime (server controls do not).

To use a server control:

Put the server control(s) in a second project (assembly) and build it.
Add your new server controls to the toolbox.
Drag-and-drop your server controls onto a form.
Drag-and-drop controls into your server control(s).

You could also create a base user control class to accomplish basically the same thing (although I couldn't coax the user control to accept child tags as controls like you want):

Create a base user control (you can do this in App_Code if you want) that adds parsed child controls between the header and footer (basically the same code as I already posted). Inherit this base control from System.Web.UI.UserControl.
Create a new user control and inherit it from the class you created in step 1.
Drop controls into your new user control.
Drop your user control onto a form.

I would suggest waiting to add design-time fanciness until you're comfortable with server controls.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum