Trap DataGrid in a each control loop

Discussion in 'ASP .Net' started by marc-andr?, Nov 17, 2004.

  1. marc-andr?

    marc-andr? Guest

    Hi,

    I have to loop on each control on a webforms...

    I use this function :

    public void LoopOnAllControls(ControlCollection coll)
    {
    foreach (System.Web.UI.Control ctrl in coll)
    {
    if(ctrl.HasControls())
    {
    LoopOnAllControls(ctrl.Controls);
    }
    else
    {
    SetTheStyleClass(ctrl);
    }

    }

    }


    public void SetTheStyleClass(Control ct)
    {
    string TypeCt;
    TypeCt = ct.GetType().ToString();

    switch (TypeCt)
    {

    case "System.Web.UI.HtmlControls.HtmlAnchor" :
    {
    (ct as HyperLink).CssClass = HyperLink;
    break;
    }

    case "System.Web.UI.HtmlControls.HtmlButton" :
    {
    (ct as Button).CssClass = BoutonDeCommande;
    break;
    }

    case "System.Web.UI.WebControls.Button" :
    {
    (ct as Button).CssClass = BoutonDeCommande;
    break;
    }

    case "System.Web.UI.HtmlControls.HtmlInputText" :
    {
    (ct as TextBox).CssClass = BoiteDeTextte ;
    break;
    }

    case "System.Web.UI.WebControls.TextBox" :
    {
    (ct as TextBox).CssClass = BoiteDeTextte ;
    break;
    }

    case "System.Web.UI.WebControls.Label" :
    {
    (ct as Label).CssClass = TexteOrdinaire ;
    break;
    }

    case "System.Web.UI.WebControls.ListBox" :
    {
    (ct as ListBox).CssClass = ListAndComboBox;
    break;
    }

    case "System.Web.UI.WebControls.DropDownList" :
    {
    (ct as DropDownList).CssClass = ListAndComboBox;
    break;
    }

    case "System.Web.UI.WebControls.HyperLink" :
    {
    (ct as HyperLink).CssClass = HyperLink;
    break;
    }




    case
    "System.Web.UI.WebControls.DataGrid" :
    {
    //They never enter here!!!!!
    DataGrid dtg = ct as DataGrid;

    for(int i=0;i<dtg.Items.Count;i++)
    {
    dtg.Items.CssClass= "texte";
    }

    break;
    }

    }
    }

    }


    the " SetTheStyleClass" is use for setting my CssClass on the
    control... anyway there's my problem

    It's works well for any control... but not with the DataGrid. I don't
    know why?? I can't trap the DataGrid control...

    Anyone have an idea!?
     
    marc-andr?, Nov 17, 2004
    #1
    1. Advertising

  2. Has the Datagrid been bound before doing this loop. Also you may want to
    revise your code. Iterating through each control can be detrimental to
    performance.

    "marc-andr?" wrote:

    > Hi,
    >
    > I have to loop on each control on a webforms...
    >
    > I use this function :
    >
    > public void LoopOnAllControls(ControlCollection coll)
    > {
    > foreach (System.Web.UI.Control ctrl in coll)
    > {
    > if(ctrl.HasControls())
    > {
    > LoopOnAllControls(ctrl.Controls);
    > }
    > else
    > {
    > SetTheStyleClass(ctrl);
    > }
    >
    > }
    >
    > }
    >
    >
    > public void SetTheStyleClass(Control ct)
    > {
    > string TypeCt;
    > TypeCt = ct.GetType().ToString();
    >
    > switch (TypeCt)
    > {
    >
    > case "System.Web.UI.HtmlControls.HtmlAnchor" :
    > {
    > (ct as HyperLink).CssClass = HyperLink;
    > break;
    > }
    >
    > case "System.Web.UI.HtmlControls.HtmlButton" :
    > {
    > (ct as Button).CssClass = BoutonDeCommande;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.Button" :
    > {
    > (ct as Button).CssClass = BoutonDeCommande;
    > break;
    > }
    >
    > case "System.Web.UI.HtmlControls.HtmlInputText" :
    > {
    > (ct as TextBox).CssClass = BoiteDeTextte ;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.TextBox" :
    > {
    > (ct as TextBox).CssClass = BoiteDeTextte ;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.Label" :
    > {
    > (ct as Label).CssClass = TexteOrdinaire ;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.ListBox" :
    > {
    > (ct as ListBox).CssClass = ListAndComboBox;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.DropDownList" :
    > {
    > (ct as DropDownList).CssClass = ListAndComboBox;
    > break;
    > }
    >
    > case "System.Web.UI.WebControls.HyperLink" :
    > {
    > (ct as HyperLink).CssClass = HyperLink;
    > break;
    > }
    >
    >
    >
    >
    > case
    > "System.Web.UI.WebControls.DataGrid" :
    > {
    > //They never enter here!!!!!
    > DataGrid dtg = ct as DataGrid;
    >
    > for(int i=0;i<dtg.Items.Count;i++)
    > {
    > dtg.Items.CssClass= "texte";
    > }
    >
    > break;
    > }
    >
    > }
    > }
    >
    > }
    >
    >
    > the " SetTheStyleClass" is use for setting my CssClass on the
    > control... anyway there's my problem
    >
    > It's works well for any control... but not with the DataGrid. I don't
    > know why?? I can't trap the DataGrid control...
    >
    > Anyone have an idea!?
    >
     
    =?Utf-8?B?TWlrZUw=?=, Nov 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    412
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    429
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    158
    Marcin Mielżyński
    Jan 20, 2006
  4. Igor Nn
    Replies:
    7
    Views:
    437
    Johnny Morrice
    May 28, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page