You cannot do this with a user control (ascx file). As there is no way to access the "inner HTML" code, that the control is wrapped around. In order to do this with a user control, you'll need to define a string property on your control, e.g. Contents which
you can then use in the markup:
You can, but it makes not really sense and the result is far away from good :)
If you try it on a blanc usercontrol, you get this:
--
Parser Error Message: Literal content ('Inner Content') is not allowed within a 'ASP.usercontrols_testusercontrol_ascx'.
--
If you apply the [ParseChildren(false)] attribute on your codebehind class of the usercontrol, it looks like:
UserControl (ascx) content:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="UserControlTest.UserControls.TestUserControl" %>
<p>
This is a test content.
</p>
If you want to create a custom control containing plaint html or other controls create it as a webcontrol using templates. Here are some articles about it:
I second toas1 suggestion, you'll need to go down the advanced path and implement your own custom control. There are a couple of good articles on the subject if you google - but I must inform you that it will not be as easy
as setting up an ascx thingy. You will have to take care of both page rendering code and designer rendering code yourself (if needed).
If this post was useful to you, please mark it as answer. Thank you!
Actually you can do what you are asking with a <System.Web.UI.UserControl>.
Using the ParseChildren attribute you can specify to allow the literal content and also which property to put it in. The property needs to be declared in the class. I have used code similar to this ...
[ParseChildren(true, "InnerContent")]
public partial class MyControl : System.Web.UI.UserControl
{
public string InnerContent
{
get { return MyPlaceHolder.InnerHtml; }
set { MyPlaceHolder.InnerHtml = value; }
}
...
}
CurtWRC
Participant
1131 Points
1026 Posts
<cc:MyControl> ... </cc:MyControl>
Jul 22, 2008 03:05 PM|LINK
Hi,
Is it possible to do something like:
<cc:MyControl id="MyControl1" runat="server">Hello World!</cc:MyControl>
Then its rendered like:
HTML......
Hello World!
HTML........
Thanks,
Curt.
johram
All-Star
28531 Points
3567 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Jul 22, 2008 11:43 PM|LINK
You cannot do this with a user control (ascx file). As there is no way to access the "inner HTML" code, that the control is wrapped around. In order to do this with a user control, you'll need to define a string property on your control, e.g. Contents which you can then use in the markup:
<cc:MyControl id="MyControl1" runat="server" Contents="Hello World!"></cc:MyControl>
Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: <cc:MyControl> ... </cc:MyControl>
Jul 23, 2008 12:37 AM|LINK
Hi there
You can, but it makes not really sense and the result is far away from good :)
If you try it on a blanc usercontrol, you get this:
--
Parser Error Message: Literal content ('Inner Content') is not allowed within a 'ASP.usercontrols_testusercontrol_ascx'.
--
If you apply the [ParseChildren(false)] attribute on your codebehind class of the usercontrol, it looks like:
UserControl (ascx) content:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="UserControlTest.UserControls.TestUserControl" %>
<p>
This is a test content.
</p>
Testpage content:
<pb:TestUserControl ID="ucTest" runat="server">
Inner Content
</pb:TestUserControl>
Html output result:
<p>
This is a test content.
</p>
Inner Content
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
CurtWRC
Participant
1131 Points
1026 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Jul 23, 2008 04:56 PM|LINK
Thanks for both of your replies.
johram, I have tried your method before, but what if I wanted to use more complex content? for example:
<cc:mycontrol>
<asp:textbox />
</cc:mycontrol>
I dont believe its possible to simply put <cc:mycontrol contents="<asp:textbox />" />
toas1
Participant
1559 Points
345 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Jul 23, 2008 08:59 PM|LINK
If you want to create a custom control containing plaint html or other controls create it as a webcontrol using templates. Here are some articles about it:
http://msdn.microsoft.com/en-us/library/3326cdex.aspx
http://www.code-magazine.com/Article.aspx?quickid=0607041
Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: <cc:MyControl> ... </cc:MyControl>
Jul 24, 2008 02:43 AM|LINK
Hi there
No, thats not possible.
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
johram
All-Star
28531 Points
3567 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Jul 24, 2008 10:36 PM|LINK
I second toas1 suggestion, you'll need to go down the advanced path and implement your own custom control. There are a couple of good articles on the subject if you google - but I must inform you that it will not be as easy as setting up an ascx thingy. You will have to take care of both page rendering code and designer rendering code yourself (if needed).
datajive
Member
4 Points
2 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Aug 04, 2008 07:04 AM|LINK
Actually you can do what you are asking with a <System.Web.UI.UserControl>.
Using the ParseChildren attribute you can specify to allow the literal content and also which property to put it in. The property needs to be declared in the class. I have used code similar to this ...
[ParseChildren(true, "InnerContent")] public partial class MyControl : System.Web.UI.UserControl { public string InnerContent { get { return MyPlaceHolder.InnerHtml; } set { MyPlaceHolder.InnerHtml = value; } } ... }In the designer ...
datajive
Member
4 Points
2 Posts
Re: <cc:MyControl> ... </cc:MyControl>
Aug 04, 2008 07:32 AM|LINK
I also saw that you wanted to have child controls inside your usercontrol. You have to indicate you want to parse as controls, so in that case use ...