473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

App_Code class: Loop through controls

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;
}
}
}
}
}
}
}
}

}
Aug 10 '06 #1
5 1794
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.




Aug 11 '06 #2
Thanks Steven,

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

Thanks, sck10

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:yL**************@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.




Aug 11 '06 #3
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.

Aug 14 '06 #4
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....

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:hA**************@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.

Aug 14 '06 #5
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

Aug 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AZ | last post by:
During the Pre-compile process of an ASP.Net 2.0 app, it compiles the code-behind & optionally the presentation files into an assembly named App_Code.dll. Can that not be renamed to a more project...
5
by: Jay Douglas | last post by:
I have a set of pages that inherit from a base class in the App_Code folder. The class looks something like: public class MyBaseClass : System.Web.UI.Page In various stages of the life cycle I...
7
by: hummh | last post by:
Hello out there, I´m making my first steps with ASP.NET 2.0 and have he following problem: I´ve implemented a Web User Control that sits in the root of my ASP.NET Website. I want to use the...
2
by: walter | last post by:
Hi, when I add a new page in my asp.net 2 project and put some controls there, everything works fine until I move the code behind into App_Code folder.--When I compile , it tells me that control...
1
by: motorhead_maniac | last post by:
anyone, how to reference server controls (without .ascx) that are in the app_code folder. The msdn documentation talks about dropping them in and using Page section of web.config to add and...
4
by: Martin Simard | last post by:
Hi all, Is there a way to rename the App_Code.dll generated from the code that belongs to the App_Code folder? We are developping several web applications that we are merging in a single...
5
by: sck10 | last post by:
Hello, I am converting a class in the App_Code folder from c# to vb and am having problems calling the sub procedure. I have created a Sub called HidePanels in the class "General". When I try...
9
by: rn5a | last post by:
Is putting a VB class file in the special directory named App_Code the same as relocating the VB class file from the App_Code directory to another directory & then using the VBC tool, compiling the...
1
by: netasp | last post by:
hi all, when adding a new web form in VS 2003, usually viewing the code behind for that form will let you write any method or function and referencing any web control placed on the form without...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.