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
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.
I believe you just need to apply a couple of attributes to the control:
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 :)
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
I also wanted to create a custom control with "innerHtml". This is what I ended up with (based partially on some of the earlier answers/comments)...
div.ascx.cs:
div.ascx:
Test page:
The suggested solutions did not work for me. I found the following solutions: Either make your user control inherit from
Panel
instead of onlyUserControl
, or if you have more than one content like in my case, make your content fields bePlaceHolder
s instead of simpleControl
s.The
[PersistenceMode(PersistenceMode.InnerProperty)]
is added to avoid XHTML validation warning.phBodyContent
andphGripContent
beingPlaceHolder
s.This way I can use my control with any content in ASPX:
I found this on here:
ASP.Net: User control with content area, it's clearly possible but I need some details
Works like a charm, but I wish I could surpress the design time message, content is not allowed between opening and closing tags, but it works at run time.
The Markup is like this:
And then in the client: