views:

92

answers:

3

Is it possible to have a custom server control with a single template (meaning the user can put any text they want) without having to require the an "ItemTemplate" like in a FormView control?

I would want the control in Source View to look like this

<foo:mycontrol runat="server" id="controlid">

User puts whatever html content they want here

</foo:mycontrol>

INSTEAD OF THIS

<foo:mycontrol runat="server" id="controlid">

<ItemTemplate>

User puts whatever html content they want here

</ItemTemplate>

</foo:mycontrol>

My custom server control needs to add 2 asp.net panel controls and the ajax collapsiblepanel control. one panel will be the expand/collapse panel and the other panel is what I would want to put the user text into and then have the collapsible panel collapse and hide the panel.

I know how to do this (at least I think I do) creating a composite server control and using ITemplate but that requires the child <ItemTemplate> tag in source view.

Any ideas?

A: 

I haven't tested this but I would think you could do this by inheriting from the literal or label control and then reading/writing to the Text property.

p.s. next time when you post a question check the preview to see if it's readable and format code with 4 spaces in-front so it's actually shown and syntax highlighted.

olle
A: 

Hmm why don't you use an approach where you just specify the controls to be collapsed. For instance your declaration could look like

<foo:mycontrol runat="server" id="controlid" TargetControlId="pnlToCollapse" />

Internally, your mycontrol gets an instance of the specified TargetControlId by using the FindControl method (here's a recursive version). The same could be done for the 2nd panel you need.

Your server control does therefore just take configuration info and doesn't render anything, but controls the rendering of the other panels in this case. It is a much more flexible solution in my eyes.

Juri
A: 

Juri,

I should have clarified that this control might be used by non-developers or developers who we do not want to require too much knowledge of setting properties so I'm trying to develop a control where they would drag it on like a panel and just enter text and maybe set one property which would be the title. I was able to create a compositecontrol which I created a title panel, the collapsible panel and utilized the asp.net ajax collapsiblepanel control. I had to add an Template though which I would prefer not to do.

Darrell

Darrell