开发者

asp.net usercontrols and nested tags

开发者 https://www.devze.com 2023-03-28 09:09 出处:网络
how can we create UserControl that is compos开发者_JS百科ed of nested tag like this : <uc:MyPanel>

how can we create UserControl that is compos开发者_JS百科ed of nested tag like this :

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

       <content>
           content here...
       </content>
    </uc:MyPanel>


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.


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


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消