2

how can we create UserControl that is composed of nested tag like this :

    <uc:MyPanel>
       <header>
           title here...
       </header>

       <content>
           content here...
       </content>
    </uc:MyPanel>
2
  • 2
    What exactly do you mean? Can you elaborate? Commented Aug 15, 2011 at 14:31
  • i have a panel with compelceted layout so to avoid copy/past the code of the panel, i decided to create custom control, wich will have sub tags to hold the title and the content of the panel Commented Aug 15, 2011 at 15:06

4 Answers 4

1

To do what you're looking for, you'll need to create a custom server control that can interprit nested tags. It will require a lot of work.

It looks like you're trying to create a layout for your site. If that is what you're trying to do you would be better to using master pages, and creating content placeholders for the header, footer, content, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

I think I understand what you are after as I was trying to find a way of doing this myself in the past. I had some complicated HTML which generated a box with a header. Rather than copying the HTML, each time I needed this box, I was hoping I could specify a header/content/whateverelse tags nested inside.

I can't find my original question, but I remember there not really being the answer I was looking for.

In the end I believe I was told to look into Custom Server Controls rather than user controls.

I hope this helps

1 Comment

i found somthing on the msdn (msdn.microsoft.com/fr-fr/library/36574bf6%28v=VS.85%29.aspx) that uses INamingContainer, and ITemplate but can't understand the logic behind it
0

Here is the solution i found, for people how want to do the same things

popup.ascx

<%@ Control Language="C#" AutoEventWireup="true" 
  CodeBehind="Popup.ascx.cs" Inherits="Dynamic.Popup" %>
<div style="width: 400px; padding: 1px; border: 1px solid #BED6F8;
  background-color: #FFFFFF; position: absolute" runat="server" id="panel">
   <div style="padding: 1px 1px 2px 5px; height: 18px; border: 1px solid #BED6F8; cursor: move;
   background: -moz-linear-gradient(top, white,#BED6F8); background-color: #BED6F8"
   runat="server" id="titlePanel">

   <asp:Literal ID="ltTitre" runat="server"></asp:Literal>

   <div style="position: absolute; right: 3px; top: 3px; z-index: 100; cursor: pointer">
       <img src="close.png" alt="fermer" onclick="$find('popUpPanel').hide();" />
   </div>

Popup.ascx.cs

 [ParseChildren(true)]
  public partial class Popup : System.Web.UI.UserControl
  {
   [TemplateContainer(typeof(MessageContainer))]
   [PersistenceMode(PersistenceMode.InnerProperty)]
   public ITemplate MessageTemplate { get; set; } 

   public string Text { get; set; }

  protected void Page_Init(object sender, EventArgs e)
  {
    ltTitre.Text = Text;

     if (MessageTemplate != null)
     {
        var i = new MessageContainer();
        MessageTemplate.InstantiateIn(i);
        PlaceHolder1.Controls.Add(i);
     }           
   }

     public class MessageContainer : Control, INamingContainer{ }
 }

the use of usercontrols

<uc:Popup ID="p" runat="server" Text="Titre1">
    <MessageTemplate>
     <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
       Test
    </MessageTemplate>
</uc:Popup>

Comments

0

I guess what you are looking for is Master pages with a Content Place holder. e.g.

 <div class="page">
    <div class="header">
        <div class="title">
            <h1>
                My ASP.NET Application
            </h1>
        </div>            
    </div>
    <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
    </div>
    <div class="clear">
    </div>
</div>
<div class="footer">

</div>

Have a look at this ASP.NET Master Pages Tutorials

1 Comment

i'm trying to create custom UserControl witch i can reuse over pages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.