Home All Groups Group Topic Archive Search About

App_Code class: Loop through controls

Author
10 Aug 2006 9:32 PM
sck10
Hello,

I am trying to add the following to a App_Code class.  The error I am
getting references "Page.Controls".  I would like to call this from my
content page which uses MasterPages

I read the following from Steven Cheng, but am having a hard time following:
In ASP.NET 2.0, the pages and usercontrols and their codebehind classes are
dynamically compiled at runtime. However, the dynamic compilation is
possible to compile the page or usercontrols's class into separate
assemblies(due to the folder structure and location of the page and
usercontrols). Thus, in your utility class (you put in App_Code or an
external assembly), it will not see those page/usercontrol classes(compiled
in a different dynamic compiled assembly). For your scenario, you want to
reference some certain usercontrol through their concrete type, I think you
may consider defining some base usercontrol class for your usercontrols,
these base classes can be put in App_Code source files or in an external
assembly(class library project), then you can make the usercontrols(in your
web application) derive from those base classes, e.g.

=============
public partial class usercontrols_HelloUC : MyBaseUserControlClass
{
........................
}
=============

Thus, you can reference the usercontrol instance type the base class type in
your utility class.


..  Any help with this would be appreciated...

sck10

public void SecurityHidePanels()
{
  foreach (Control ctlMaster in Page.Controls)
  {                                   // Page
    if (ctlMaster is MasterPage)
    {                                 // MasterPage
      foreach (Control ctlForm in ctlMaster.Controls)
      {
        if (ctlForm is HtmlForm)
        {                            // HtmlForm
          foreach (Control ctlContent in ctlForm.Controls)
          {
            if (ctlContent is ContentPlaceHolder)
            {                       // ContentPlaceHolder
              foreach (Control ctlChild in ctlContent.Controls)
              { //Hide All Panel
                if (ctlChild is Panel)
                {
                  ctlChild.Visible = false;
                }
              }
            }
          }
        }
      }
    }
  }

}

Author
11 Aug 2006 7:00 AM
Steven Cheng[MSFT]
Hello Steve,

From the code and error info you provided, I can get that you're creating a
custom helper class(put in App_code directory) and it the custom class's
method, you directly use the Page class and its members to loop through the
page's control collection ,but encountered errors, correct?

As for the App_Code, the class put in it is just normal custom classes,
they has not particular context and is as normal as other custom classes
put in a separate Class Library project.  Therefore, if you want to define
a custom Helper class that loop through Page's control collection, I
suggest you define the method to accept an input parameter of Page class ,
e.g:

public class HelperClass
{
   public void LoopControlsInPage(Page page)
   {
         ................
   }
}



BTW, are you using this custom class in page's codebehind? If this is the
case, it is easy to get the Page's reference and pass it into such custom
helper function.


In addition, since you're writing code to loop through the Page's control
collection, I suggest you turn on the Page's output Trace which will
display the pages' complete Control Tree at the bottom of the page output
.e.g.

<%@ Page ....................      Trace="true" %>


this is quite useful at development/test time. 

Hope this helps. Please let me know if you have anything unclear or
anything else you wonder.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
Author
11 Aug 2006 4:10 PM
sck10
Thanks Steven,

How would you supply the page when calling this from a content page?
class_General.SecurityHidePanels(?????);

Thanks, sck10



Show quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:yLGczPRvGHA.1992@TK2MSFTNGXA01.phx.gbl...
> Hello Steve,
>
> From the code and error info you provided, I can get that you're creating
> a
> custom helper class(put in App_code directory) and it the custom class's
> method, you directly use the Page class and its members to loop through
> the
> page's control collection ,but encountered errors, correct?
>
> As for the App_Code, the class put in it is just normal custom classes,
> they has not particular context and is as normal as other custom classes
> put in a separate Class Library project.  Therefore, if you want to define
> a custom Helper class that loop through Page's control collection, I
> suggest you define the method to accept an input parameter of Page class ,
> e.g:
>
> public class HelperClass
> {
>   public void LoopControlsInPage(Page page)
>   {
>         ................
>   }
> }
>
>
>
> BTW, are you using this custom class in page's codebehind? If this is the
> case, it is easy to get the Page's reference and pass it into such custom
> helper function.
>
>
> In addition, since you're writing code to loop through the Page's control
> collection, I suggest you turn on the Page's output Trace which will
> display the pages' complete Control Tree at the bottom of the page output
> e.g.
>
> <%@ Page ....................      Trace="true" %>
>
>
> this is quite useful at development/test time.
>
> Hope this helps. Please let me know if you have anything unclear or
> anything else you wonder.
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
>
>
>
>
>
>
>
>
>
>
Author
14 Aug 2006 1:48 AM
Steven Cheng[MSFT]
Hi Steve,

We can use the "Page" property as long as the code is executing in a
certain pages' code(no matter it is Master Page or Content page derived
from a master).  Actually, for content page, it will include the master
page(it applies ) as a child control. 

For example, in the content page's Page_Load event, you can call your
helper class's function as below:

void Page_Load(.....)
{
    class_General.SecurityHidePanels( this.Page );
}

Please let me know if this helps or if you have any particular concerns
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
Author
14 Aug 2006 1:54 PM
sck10
Hi Steven,

I finally figured it out last night.  Thanks for all your help though.
Moving from vb to c# has been somewhat of a struggle (smile).

Cheers....

Show quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:hAMAWP0vGHA.2676@TK2MSFTNGXA01.phx.gbl...
> Hi Steve,
>
> We can use the "Page" property as long as the code is executing in a
> certain pages' code(no matter it is Master Page or Content page derived
> from a master).  Actually, for content page, it will include the master
> page(it applies ) as a child control.
>
> For example, in the content page's Page_Load event, you can call your
> helper class's function as below:
>
> void Page_Load(.....)
> {
>    class_General.SecurityHidePanels( this.Page );
> }
>
> Please let me know if this helps or if you have any particular concerns
> here.
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
Author
15 Aug 2006 1:49 AM
Steven Cheng[MSFT]
Hi Steve,

Glad that you've figured it out. I've seen some guys suggest you the
"Reflector" tool, I also agree that it is a good tool if you have some code
of a single language  want to convert it to other different .net languages.

Good luck!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

AddThis Social Bookmark Button