我有這樣的用戶控件: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: <<a href=\"{4}\" rel=\"nofollow\" style=\"text-decoration: none; color: black;\">{4}</a>>", 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/>標記未知。我只想解析用戶控件的內部內容,創建類的實例(Web或Book)並將它們添加到控件的參考文獻屬性。