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
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 PlaceHolder
s instead of simple Control
s.
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);
}
}
phBodyContent
and phGripContent
being PlaceHolder
s.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With