views:

48

answers:

2

Hello, I have this user control:

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

with behind code:

[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));
    }
}

and I call it like this:

<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&amp;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>

Control is also registered in web.config file. But <asp:Web /> and <asp:Book /> tags are unknown. I want just parse inner content of user control, create instances of classes (Web or Book) and add them to the control's References property.

+1  A: 

You haven't defined Web or Book as UserControls. They need to be defined as such before you can use them that way. Note that they can still derive from Reference, but Reference itself will need to derive from UserControl. You'll also need to register them in your page before you can use them.

tvanfosson
+1  A: 

Hey,

Check out this: http://www.west-wind.com/WebLog/posts/200.aspx, this section: Collection Member Level, for more information on defining collections within controls. You need to also set the DesignerSerializationVisibility attribute for your case.

There is also this: http://www.west-wind.com/WebLog/posts/7060.aspx http://stackoverflow.com/questions/123616/how-do-you-build-an-asp-net-custom-control-with-a-collection-property

I know that these deal with custom controls, but the two work the same, and I'm not sure how much support user controls do support custom children.

Brian