Utilizzare l'attributo ParseChildren

ParseChildrenAttribute

Questo attributo utilizzato in fase di progettazione indica al parser della pagina come interpretare il contenuto nidificato all'interno del tag di un controllo asp.net . 

E' un attributo molto utile se non indispensabile soprattutto quando si vuole inserire in un controllo custom una proprietà di tipo collection.  Per capire la sua funzione si pensi al controllo asp:Table che si aspetta come proprietà annidate n asp:TableRow che a sua volta si aspetta n asp:TableCell . 

 

Vediamo un esempio pratico di come si utilizza, di seguito tutto il codice per la creazione di un controllo che accetta come proprietà una lista di Customer che come risultato finale saranno reiderizzati in una tabella.

 

 

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections.Generic;

 

namespace Samples.AspNet.CS.Controls

{

 

    public class Customer

    {

        public Customer() : this("", "", "") { }

 

        public Customer(string companyName,string phone, string webSite)

        {

            this.CompanyName = companyName;

            this.Phone = phone;

            this.WebSite = webSite;

        }

        public string CompanyName { get; set; }

 

        public string Phone  { get; set; }

 

        public string WebSite{ get; set; }

    }

 

    //Impostando a true il primo argomento si indica al parser di interpretare come

    //proprietà gli elementi inclusi nel tag del controllo, mentre il secondo argomento

    //indica la proprietà predefinita relativa ai valori passati.

    [ParseChildren(true, "Customers")]

    public class DemoParseChildren : Control

    {

        public string Title{ get; set; }

 

       

        List<Customer> _customers = new List<Customer>();

        public List<Customer> Customers

        {

            get

            {

                return _customers;

            }

        }

 

        protected override void CreateChildControls()

        {

            //Titolo: recuperato dalla proprietà Title

            Literal lit = new Literal();

            lit.Text = string.Format("<h2>{0}</h2>", this.Title);

            Controls.Add(lit);

 

            Table table = new Table();

            TableRow htr = new TableRow();

 

            table.BorderWidth = 3;

            table.BackColor = System.Drawing.Color.LightGray;

            table.BorderStyle = BorderStyle.Solid;

           

            //Intestazione Colonne

            TableHeaderRow headerRow = new TableHeaderRow();

            TableHeaderCell headerName = new TableHeaderCell();

            headerName.Text = "Company Name";

            headerRow.Cells.Add(headerName);

 

            TableHeaderCell headerPhone = new TableHeaderCell();

            headerPhone.Text = "Phone";

            headerRow.Cells.Add(headerPhone);

 

            TableHeaderCell headerWebSite = new TableHeaderCell();

            headerWebSite.Text = "Web Site";

            headerRow.Cells.Add(headerWebSite);

 

            table.Rows.Add(headerRow);

            table.Style.Add("width", "100%");

            //Inserisci tutti i clienti: ogni riga per ogni cliente

            foreach (Customer customer in Customers )

            {

                TableRow tr = new TableRow();

 

                TableCell cell1 = new TableCell();

                cell1.Text = customer.CompanyName;

                cell1.Style.Add("border", "1px solid #ffffff");

                tr.Cells.Add(cell1);

 

                TableCell cell2 = new TableCell();

                cell2.Text = customer.Phone;

                cell2.Style.Add("border", "1px solid #ffffff");

                tr.Cells.Add(cell2);

 

                HyperLink linkSite = new HyperLink();

                linkSite.Text = customer.WebSite;

                linkSite.NavigateUrl = customer.WebSite;

 

                TableCell cell3 = new TableCell();

                cell3.Controls.Add(linkSite);

                cell3.Style.Add("border", "1px solid #ffffff");

                tr.Cells.Add(cell3);

 

                table.Rows.Add(tr);

            }

            Controls.Add(table);

        }

    }

}

 

All'interno della pagina aspx:

 

<%@ Page Language="C#" Debug="true" %>

<%@ Register TagPrefix="AspSample"  Namespace="Samples.AspNet.CS.Controls" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html>

<head id="Head1" runat="server">

    <title>ParseChildrenAttribute Example</title>

</head>

<body>

    <form id="Form1" runat="server">

        <AspSample:DemoParseChildren runat="server" ID="DemoParseChildren" Title="Demo ParseChildrenAttribute">

            <AspSample:Customer CompanyName ="N.D.R. Impianti" phone="02 324456" WebSite="http://www.ndr.it" />

            <AspSample:Customer CompanyName ="Moda Più" phone="0332 76309" WebSite="http://www.modapiu.com"  />

            <AspSample:Customer CompanyName ="Falegnameria Italia" phone="031 980743" WebSite="http://www.falegnameriaitalia.it" />

        </AspSample:DemoParseChildren>

    </form>

</body>

</html>

Ed ecco il risultato finale:

Visualizza

posted @ giovedì 5 giugno 2008 15:48

Print
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011