I faced with my understanding about “ParseChildrenAttribute†class

P

Pab

Hi,
I faced with my understanding about “ParseChildrenAttribute†class using. My
example is following: two classes are created: Employee and
CollectionEmployee. To CollectionEmployee class I applied ParseChildren
attribyte as “[ParseChildren(typeof(Employee))]â€, but I don’t know how to
collect Employee in CreateChildControls method (Controls collection is
empty). If I’ll replace line “[ParseChildren(typeof(Employee))]’ with
“[ParseChildren(true, "Employees")]†so I can access Employee instances in
“Employees’ Array List. Not bad, but in this case when I try to write tag
under “<CollectionEmployee >†tag in VS editor, IntelliSense offer list of in
all classes in “t†namespace – it’s not right. So both cases are no
convenient, can anybody help me with this task?


Namespace t
{
public sealed class Employee: WebControl
{…}

[ParseChildren(typeof(Employee))]
public sealed class CollectionEmployee: WebControl
{..
private ArrayList employees = new ArrayList();
public ArrayList Employees
{
get
{
return employees;
}
}
protected override void CreateChildControls()
{
??????????
}
..
}
}

<body>
<form id="form1" runat="server">
<t.CollectionEmployee id=" CollectionChildControl1" runat="server">

<t.Employee Name="Employee 1"
Title="Title 1" />
<t.Employee Name="Employee 2"
Title="Title 2" />
</t.CollectionEmployee >
…..
 
T

Teemu Keiski

Hi,

if you have at least .NET 2.0 (generics) at your disposal, you could try to
use Generic List typed to Employee instead of ArrayList. I have VS2008, and
following code worked pretty fine with it (Intellisense suggests only
Employee control as child elements to CollectionEmployee)

If you use VS2005, the System.Linq etc namespace imports won't work for you


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace WebApplication2
{
public class Employee : WebControl
{
public string Name
{
get;
set;
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//as an example just writing the name out
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(Name);
writer.RenderEndTag();
}
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
[ParseChildren(typeof(Employee),DefaultProperty="Employees",ChildrenAsProperties=true)]
public class CollectionEmployee : WebControl, INamingContainer
{
private List<Employee> _employees = null;
public List<Employee> Employees
{
get
{
if (_employees == null)
_employees = new List<Employee>();

return _employees;
}
}

protected override void CreateChildControls()
{
base.CreateChildControls();
foreach (Employee e in Employees)
{
Controls.Add(e);
Controls.Add(new LiteralControl("Something here"));
}
}
}
}



--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

Pab said:
Hi,
I faced with my understanding about "ParseChildrenAttribute" class using.
My
example is following: two classes are created: Employee and
CollectionEmployee. To CollectionEmployee class I applied ParseChildren
attribyte as "[ParseChildren(typeof(Employee))]", but I don't know how to
collect Employee in CreateChildControls method (Controls collection is
empty). If I'll replace line "[ParseChildren(typeof(Employee))]' with
"[ParseChildren(true, "Employees")]" so I can access Employee instances in
"Employees' Array List. Not bad, but in this case when I try to write tag
under "<CollectionEmployee >" tag in VS editor, IntelliSense offer list of
in
all classes in "t" namespace - it's not right. So both cases are no
convenient, can anybody help me with this task?


Namespace t
{
public sealed class Employee: WebControl
{.}

[ParseChildren(typeof(Employee))]
public sealed class CollectionEmployee: WebControl
{..
private ArrayList employees = new ArrayList();
public ArrayList Employees
{
get
{
return employees;
}
}
protected override void CreateChildControls()
{
??????????
}
..
}
}

<body>
<form id="form1" runat="server">
<t.CollectionEmployee id=" CollectionChildControl1" runat="server">

<t.Employee Name="Employee 1"
Title="Title 1" />
<t.Employee Name="Employee 2"
Title="Title 2" />
</t.CollectionEmployee >
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top