locked
<cc:MyControl> ... </cc:MyControl> RRS feed

  • Question

  • User294545519 posted

     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.

    Tuesday, July 22, 2008 11:05 AM

Answers

  • User1191518856 posted

    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).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 6:36 PM

All replies

  • User1191518856 posted

    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>

    Tuesday, July 22, 2008 7:43 PM
  • User481221548 posted

    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

     

     

    Tuesday, July 22, 2008 8:37 PM
  • User294545519 posted

     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 />" />

    Wednesday, July 23, 2008 12:56 PM
  • User-2004844803 posted

    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

    Wednesday, July 23, 2008 4:59 PM
  • User481221548 posted

    Hi there 

     I dont believe its possible to simply put <cc:mycontrol contents="<asp:textbox />" />


    No, thats not possible.
    Wednesday, July 23, 2008 10:43 PM
  • User1191518856 posted

    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).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 6:36 PM
  • User-1187701410 posted

    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 ...

     

    <div id="MyPlaceHolder" runat="server"></div>
      

     

     

    Monday, August 4, 2008 3:04 AM
  • User-1187701410 posted

     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 ...
     

    [ParseChildren(false, "InnerContent")]
      
    Monday, August 4, 2008 3:32 AM