ASP.NET 2.0 User Control that implements a template

Discussion in 'ASP .Net Building Controls' started by Wes, Nov 18, 2005.

  1. Wes

    Wes Guest

    I have created a user control in ASP.NET 2.0 that utilizes ITemplate. The
    control has no issues (displays fine in a browser) until I try to go into
    design mode on the aspx page that is implementing the control. The aspx page
    says "Can not switch views: Content is not allowed between the openening and
    closing tags for element 'Section'". I have include a simplified version of
    the control and the implementation on the aspx page. Any insite into enabling
    the user control for view in the design mode of an aspx page would be greatly
    appreciated.

    user control:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Section.ascx.cs"
    Inherits="Common_FormSections_Section" %>

    <hr />
    <asp:placeHolder ID="contentholder" runat="server"></asp:placeHolder>
    <hr />

    codebehind:

    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Common_FormSections_Section : System.Web.UI.UserControl
    {

    protected override void CreateChildControls()
    {
    if (null != Header)
    Content.InstantiateIn(contentholder);
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private ITemplate _contentTemplate = null;

    [TemplateInstance(TemplateInstance.Single)]
    public ITemplate Content
    {
    get
    {
    return _contentTemplate;
    }
    set
    {
    _contentTemplate = value;
    }
    }

    }

    aspx implimentation:
    The register tag has been intentional excluded

    <uc1:Section ID="Section1" runat="server">
    <Content>
    This is content

    </Content>

    </uc1:Section>

    Thanks in advance
    Wes, Nov 18, 2005
    #1
    1. Advertising

  2. Wes

    Wes Guest

    The CreateChildControls method should have read

    protected override void CreateChildControls()
    {
    if (null != Content)
    Content.InstantiateIn(contentholder);
    }
    }


    "Wes" wrote:

    > I have created a user control in ASP.NET 2.0 that utilizes ITemplate. The
    > control has no issues (displays fine in a browser) until I try to go into
    > design mode on the aspx page that is implementing the control. The aspx page
    > says "Can not switch views: Content is not allowed between the openening and
    > closing tags for element 'Section'". I have include a simplified version of
    > the control and the implementation on the aspx page. Any insite into enabling
    > the user control for view in the design mode of an aspx page would be greatly
    > appreciated.
    >
    > user control:
    > <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Section.ascx.cs"
    > Inherits="Common_FormSections_Section" %>
    >
    > <hr />
    > <asp:placeHolder ID="contentholder" runat="server"></asp:placeHolder>
    > <hr />
    >
    > codebehind:
    >
    > using System.Collections;
    > using System.Web;
    > using System.Web.Security;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.WebControls.WebParts;
    > using System.Web.UI.HtmlControls;
    >
    > public partial class Common_FormSections_Section : System.Web.UI.UserControl
    > {
    >
    > protected override void CreateChildControls()
    > {
    > if (null != Header)
    > Content.InstantiateIn(contentholder);
    > }
    >
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > }
    >
    > private ITemplate _contentTemplate = null;
    >
    > [TemplateInstance(TemplateInstance.Single)]
    > public ITemplate Content
    > {
    > get
    > {
    > return _contentTemplate;
    > }
    > set
    > {
    > _contentTemplate = value;
    > }
    > }
    >
    > }
    >
    > aspx implimentation:
    > The register tag has been intentional excluded
    >
    > <uc1:Section ID="Section1" runat="server">
    > <Content>
    > This is content
    >
    > </Content>
    >
    > </uc1:Section>
    >
    > Thanks in advance
    Wes, Nov 18, 2005
    #2
    1. Advertising

  3. Wes

    Brock Allen Guest

    UserControls (ASCX files) aren't typically used this way. Usually a Composite
    control is used. IOW, and IMO, you don't need the ASCX portion of this control.
    In your CreateChildControls add a LiteralControl with the <hr>, then add
    your content, then add another LiteralControl with the last <hr>.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > I have created a user control in ASP.NET 2.0 that utilizes ITemplate.
    > The control has no issues (displays fine in a browser) until I try to
    > go into design mode on the aspx page that is implementing the control.
    > The aspx page says "Can not switch views: Content is not allowed
    > between the openening and closing tags for element 'Section'". I have
    > include a simplified version of the control and the implementation on
    > the aspx page. Any insite into enabling the user control for view in
    > the design mode of an aspx page would be greatly appreciated.
    >
    > user control:
    > <%@ Control Language="C#" AutoEventWireup="true"
    > CodeFile="Section.ascx.cs"
    > Inherits="Common_FormSections_Section" %>
    > <hr />
    > <asp:placeHolder ID="contentholder" runat="server"></asp:placeHolder>
    > <hr />
    > codebehind:
    >
    > using System.Collections;
    > using System.Web;
    > using System.Web.Security;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.WebControls.WebParts;
    > using System.Web.UI.HtmlControls;
    > public partial class Common_FormSections_Section :
    > System.Web.UI.UserControl {
    >
    > protected override void CreateChildControls()
    > {
    > if (null != Header)
    > Content.InstantiateIn(contentholder);
    > }
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > }
    >
    > private ITemplate _contentTemplate = null;
    >
    > [TemplateInstance(TemplateInstance.Single)]
    > public ITemplate Content
    > {
    > get
    > {
    > return _contentTemplate;
    > }
    > set
    > {
    > _contentTemplate = value;
    > }
    > }
    > }
    >
    > aspx implimentation:
    > The register tag has been intentional excluded
    > <uc1:Section ID="Section1" runat="server">
    > <Content>
    > This is content
    > </Content>
    >
    > </uc1:Section>
    >
    > Thanks in advance
    >
    Brock Allen, Nov 18, 2005
    #3
  4. Wes

    Wes Guest

    Thank you for the response. I am only trying to implement an example from MSDN

    http://msdn.microsoft.com/library/e...onCreatingTemplatedUserControl.asp?frame=true

    of course in the end I will implement something far more complex.

    The issue is not the implementation but rather displaying the aspx page in
    design mode.

    "Brock Allen" wrote:

    > UserControls (ASCX files) aren't typically used this way. Usually a Composite
    > control is used. IOW, and IMO, you don't need the ASCX portion of this control.
    > In your CreateChildControls add a LiteralControl with the <hr>, then add
    > your content, then add another LiteralControl with the last <hr>.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    > > I have created a user control in ASP.NET 2.0 that utilizes ITemplate.
    > > The control has no issues (displays fine in a browser) until I try to
    > > go into design mode on the aspx page that is implementing the control.
    > > The aspx page says "Can not switch views: Content is not allowed
    > > between the openening and closing tags for element 'Section'". I have
    > > include a simplified version of the control and the implementation on
    > > the aspx page. Any insite into enabling the user control for view in
    > > the design mode of an aspx page would be greatly appreciated.
    > >
    > > user control:
    > > <%@ Control Language="C#" AutoEventWireup="true"
    > > CodeFile="Section.ascx.cs"
    > > Inherits="Common_FormSections_Section" %>
    > > <hr />
    > > <asp:placeHolder ID="contentholder" runat="server"></asp:placeHolder>
    > > <hr />
    > > codebehind:
    > >
    > > using System.Collections;
    > > using System.Web;
    > > using System.Web.Security;
    > > using System.Web.UI;
    > > using System.Web.UI.WebControls;
    > > using System.Web.UI.WebControls.WebParts;
    > > using System.Web.UI.HtmlControls;
    > > public partial class Common_FormSections_Section :
    > > System.Web.UI.UserControl {
    > >
    > > protected override void CreateChildControls()
    > > {
    > > if (null != Header)
    > > Content.InstantiateIn(contentholder);
    > > }
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > }
    > >
    > > private ITemplate _contentTemplate = null;
    > >
    > > [TemplateInstance(TemplateInstance.Single)]
    > > public ITemplate Content
    > > {
    > > get
    > > {
    > > return _contentTemplate;
    > > }
    > > set
    > > {
    > > _contentTemplate = value;
    > > }
    > > }
    > > }
    > >
    > > aspx implimentation:
    > > The register tag has been intentional excluded
    > > <uc1:Section ID="Section1" runat="server">
    > > <Content>
    > > This is content
    > > </Content>
    > >
    > > </uc1:Section>
    > >
    > > Thanks in advance
    > >

    >
    >
    >
    Wes, Nov 18, 2005
    #4
  5. Wes

    Brock Allen Guest

    Sure, I understand. The reason it doesn't work is that the ASCX is the concrete
    class that's being put on the page and it doesn't have the [ParseChildren(true)]
    attribute (since the class is gen'd form the ASCX) and all the other things
    like ControlDesigers that are necessary to make the deisgn mode work. If
    you just make your own class (in a .cs file) and don't use the ASCX you'll
    be able to put those attrribues on yourself. Check out "Developing Microsoft
    ASP.NET Server Controls and Components", by Nikhil Kothari and Vandana Datye
    from MS Press as it describes in detail all of what needs to be done to support
    the designer.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > Thank you for the response. I am only trying to implement an example
    > from MSDN
    >
    > http://msdn.microsoft.com/library/en-us/cpguide/html/cpconCreatingTemp
    > latedUserControl.asp?frame=true
    >
    > of course in the end I will implement something far more complex.
    >
    > The issue is not the implementation but rather displaying the aspx
    > page in design mode.
    >
    > "Brock Allen" wrote:
    >
    >> UserControls (ASCX files) aren't typically used this way. Usually a
    >> Composite control is used. IOW, and IMO, you don't need the ASCX
    >> portion of this control. In your CreateChildControls add a
    >> LiteralControl with the <hr>, then add your content, then add another
    >> LiteralControl with the last <hr>.
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> I have created a user control in ASP.NET 2.0 that utilizes
    >>> ITemplate. The control has no issues (displays fine in a browser)
    >>> until I try to go into design mode on the aspx page that is
    >>> implementing the control. The aspx page says "Can not switch views:
    >>> Content is not allowed between the openening and closing tags for
    >>> element 'Section'". I have include a simplified version of the
    >>> control and the implementation on the aspx page. Any insite into
    >>> enabling the user control for view in the design mode of an aspx
    >>> page would be greatly appreciated.
    >>>
    >>> user control:
    >>> <%@ Control Language="C#" AutoEventWireup="true"
    >>> CodeFile="Section.ascx.cs"
    >>> Inherits="Common_FormSections_Section" %>
    >>> <hr />
    >>> <asp:placeHolder ID="contentholder"
    >>> runat="server"></asp:placeHolder>
    >>> <hr />
    >>> codebehind:
    >>> using System.Collections;
    >>> using System.Web;
    >>> using System.Web.Security;
    >>> using System.Web.UI;
    >>> using System.Web.UI.WebControls;
    >>> using System.Web.UI.WebControls.WebParts;
    >>> using System.Web.UI.HtmlControls;
    >>> public partial class Common_FormSections_Section :
    >>> System.Web.UI.UserControl {
    >>> protected override void CreateChildControls()
    >>> {
    >>> if (null != Header)
    >>> Content.InstantiateIn(contentholder);
    >>> }
    >>> protected void Page_Load(object sender, EventArgs e)
    >>> {
    >>> }
    >>> private ITemplate _contentTemplate = null;
    >>>
    >>> [TemplateInstance(TemplateInstance.Single)]
    >>> public ITemplate Content
    >>> {
    >>> get
    >>> {
    >>> return _contentTemplate;
    >>> }
    >>> set
    >>> {
    >>> _contentTemplate = value;
    >>> }
    >>> }
    >>> }
    >>> aspx implimentation:
    >>> The register tag has been intentional excluded
    >>> <uc1:Section ID="Section1" runat="server">
    >>> <Content>
    >>> This is content
    >>> </Content>
    >>> </uc1:Section>
    >>>
    >>> Thanks in advance
    >>>
    Brock Allen, Nov 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. owais
    Replies:
    3
    Views:
    1,029
    Francois Bonin [C# MVP]
    Jun 23, 2005
  2. Lee
    Replies:
    1
    Views:
    970
    Charles Bailey
    May 9, 2004
  3. Andrew Connell
    Replies:
    3
    Views:
    2,788
    Andrew Connell
    Oct 22, 2003
  4. JJ
    Replies:
    0
    Views:
    334
  5. Replies:
    2
    Views:
    480
    sloan
    Feb 2, 2008
Loading...

Share This Page