views:

463

answers:

4

I want to build a user control suppose MyDiv.ascx. This control renders the div tag and do few more code behind stuff like adding few attributes etc which is not a matter of concern here. The problem is I want text between the opening and closing tags of the user control. Eg:

The text goes here with some other HTML tags.

So when do something like this I get a parsing error while running the website. Also VS2008 warns me by saying "Content is not allowed between the opening and closing tags for element MyDiv".

  • Question 1: Can I do something like this i.e. text/markup between opening and closing tags of a user control?

  • Question 2: If yes, how

A: 

Add a Text property to your control and linked this text property to a label run at server that will be between the opening and closing div.

Gregoire
That's what I don't want to do...I want it to behave like normal tags...
Manish
+2  A: 

I believe you just need to apply a couple of attributes to the control:

[ParseChildren(false)]
[PersistChildren(true)]
public class MyDiv : UserControl
{
    ...

You may then need to override AddedControl - I'm not sure.

Put it this way - that's what works for the one and only user control I've ever written :)

Jon Skeet
Wow...that worked like a charm...But...can u plz tell me how can I get that inner html as I'm not getting this kind of property..like **this.InnerHtml or this.Children**..???
Manish
You can use [ParseChildren(true, "Body")] with a property called "Body" of type string. This will put the contents of the InnerHtml and put it into whatever property you named as your second argument in the ParseChildren annotation.
Jason Sperske
A: 

You might want to be careful, what if you put a server control in the content area...

You might just want to make a control inherit from a panel and override any methods you need to adjust? Might be easier or harder depending on what you need to customize

public class MyDiv : Panel
{

}
BigBlondeViking
I can't do this I guess...bcoz it's already inheriting from UserControl class...so won't be able to inherit from other class...is their any Interface I can implement???
Manish
+2  A: 

The suggested solutions did not work for me. I found the following solutions: Either make your user control inherit from Panel instead of only UserControl, or if you have more than one content like in my case, make your content fields be PlaceHolders instead of simple Controls.

The [PersistenceMode(PersistenceMode.InnerProperty)] is added to avoid XHTML validation warning.

public partial class DrawerControl : UserControl
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public PlaceHolder BodyContent { get; set; }
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public PlaceHolder GripContent { get; set; }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        phBodyContent.Controls.Add(BodyContent);
        phGripContent.Controls.Add(GripContent);
    }
}

phBodyContentand phGripContent being PlaceHolders.

This way I can use my control with any content in ASPX:

<local:Drawer ID="ctlDrawer" runat="server">
    <BodyContent>
        <!--Insert any ASP content here-->
    </BodyContent>
    <GripContent>
        <!--Insert any ASP content here-->
    </GripContent>
</local:Drawer>
Mart
May be going for a Composite Control(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositecontrol.aspx) a good solution in your case. What do u say Mart?
Manish
I added more details in my answer to illustrate the flexibility of this solution. I switch to CompositeControls when I have to embed the control into an assembly with its resources for an easier distribution.
Mart