2010-01-05 143 views
0

我有這樣的用戶控件:ASP.NET用戶控件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="cite.ascx.cs" Inherits="cite" %> 

背後代碼:

[ParseChildren(typeof(Reference), DefaultProperty = "References", ChildrenAsProperties = true)] 
public partial class cite : System.Web.UI.UserControl { 

    public cite() { 
     this.References = new ReferenceCollection(); 
    } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public ReferenceCollection References { get; private set; } 

    protected override void Render(HtmlTextWriter writer) { 
     writer.Write("<ul>"); 
     this.References.ForEach(reference => reference.Render(writer)); 
     writer.Write("</ul>"); 
    } 
} 

public class ReferenceCollection : List<Reference> { } 

public abstract class Reference { 
    public string Title { get; set; } 
    public abstract void Render(HtmlTextWriter writer); 
} 

public class Book : Reference { 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Edition { get; set; } 
    public string Place { get; set; } 
    public string LocalTitle { get; set; } 
    public int LocalPublished { get; set; } 
    public string ISBN { get; set; } 
    public int Page { get; set; } 

    public override void Render(HtmlTextWriter writer) { 
     writer.Write(string.Format("<li><cite><span style=\"text-transform: uppercase;\">{0}</span>, {1}. <i>{3}</i>. {4}. vyd. {5} : {6}, {7}. ISBN {8} S. {9}.</cite></li>", LastName, FirstName, Title, Edition, Place, LocalTitle, LocalPublished, ISBN, Page)); 
    } 
} 

public class Web : Reference { 
    public string Site { get; set; } 
    public DateTime Published { get; set; } 
    public DateTime Cited { get; set; } 
    public Uri URL { get; set; } 

    public override void Render(HtmlTextWriter writer) { 
     writer.Write(string.Format("<li><cite><i>{0}</i> [online]. {1}, {2}, [cit. {3}]. URL: &lt;<a href=\"{4}\" rel=\"nofollow\" style=\"text-decoration: none; color: black;\">{4}</a>&gt;", Title, Site, Published.ToSlashedString(), Cited.ToSlashedString(), URL)); 
    } 
} 

,我這樣稱呼它:

<asp:references runat="server"> 
    <asp:Web Title="F-14 Tomcat" Site="Wikipedia" Published="2005-09-23" Cited="2005-10-08" URL="http://en.wikipedia.org/w/index.php?title=F-14_Tomcat&oldid=19416063" /> 
    <asp:Book FirstName="David" LastName="Donald" Title="The Pocket Guide to Military Aircraft and the World's Airforces" Edition="1" Place="Praha" LocalTitle="Kapesní encyklopedie Vojenská letadla" LocalPubliched="2002" ISBN="80-7181-701-5" Page="76" /> 
</asp:references> 

控制也註冊在web.config文件中。但是< asp:Web/>和< asp:Book/>標記未知。我只想解析用戶控件的內部內容,創建類的實例(WebBook)並將它們添加到控件的參考文獻屬性。

回答

1

您尚未將Web或Book定義爲UserControl。他們需要被定義爲這樣纔可以使用它們。請注意,它們仍然可以從Reference中派生,但Reference必須從UserControl派生。您還需要在您的頁面中使用register,然後才能使用它們。

1

看看這個:http://www.west-wind.com/WebLog/posts/200.aspx,這部分:集合成員級別,更多關於在控件中定義集合的信息。您還需要爲您的案例設置DesignerSerializationVisibility屬性。

也有這樣的: http://www.west-wind.com/WebLog/posts/7060.aspx How do you build an ASP.NET custom control with a collection property?

我知道,這些處理自定義控件,但兩人的工作是相同的,我不知道多少支持用戶控件執行如何支持自定義的孩子。