Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / May 2008

Tip: Looking for answers? Try searching our database.

I faced with my understanding ab    out “ParseChildrenAttribute” cla    ss

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pab - 22 May 2008 19:43 GMT
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 >
…..
Teemu Keiski - 22 May 2008 20:01 GMT
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"));
           }
       }
   }
}

Signature

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

> Hi,
> I faced with my understanding about "ParseChildrenAttribute" class using.
[quoted text clipped - 45 lines]
>          </t.CollectionEmployee >
> ...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.