Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Page.Controls Collection

  Asked By: Lambodar    Date: May 27    Category: Asp.net    Views: 1876
  

I'm trying to iterate over the Page.Controls
Collection in a foreach statement contained in the Page_PreRender
Event. However, when I do this, not all of my controls are included.
The form and literal controls are listed, but no System.Web.UI.WebControls.Image
or ImageButtons are. Can anyone tell me why this is?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Keith Marshall     Answered On: May 27

If you


System.Web.UI.WebControls.Image Image2;
Image2 = new System.Web.UI.WebControls.Image();
this.Controls.Add(Image2);
Image2.ID="hei";
System.Web.UI.WebControls.Image Image1;
Image1 = new System.Web.UI.WebControls.Image();
this.Controls.Add(Image1);
Image1.ID = "Im1";

In page  load then they do show up.

It's the "new" keyword that does it.

When you use the designer you are in design time so they won't show up, in
effect the "new" bit added to them at runtime if you use the designer.

foreach(Control cn in Page.Controls )
{
Response.Write("Controls " + cn.GetType().ToString() + "<br>");
}

or

IEnumerator myEnumerator = Page.Controls.GetEnumerator();
while ( myEnumerator.MoveNext() )
Response.Write(myEnumerator.Current.GetType().ToString() + "<br>");


I know what you've done ... you say Literals show up. They don't. asp.NET, with
a blank page, comes up with 3 of them.

System.Web.UI.ResourceBasedLiteralControl
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.LiteralControl

The form, the literal control is viewstate what is the other ? The other,
believe it or not is the <html></html> tag

Nothing you add from toolbox will show here .... you must add them
programmatically to show up.

I know what you did, you added a literal from the toolbox and then an Image and
then you had the 3 above coming up ... you assumed (after deleting the Image)
that the literal DID show up.

Don't take this as a expert talking, but its about right - it's the "new"
keyword that is what it's AAAAALL about. Bear that in mind, it comes up around
the place and is very important.

 
Answer #2    Answered By: Timothy Patterson     Answered On: May 27

Are your image contols located inside a user control? If so have a look at
this thread:
www.dotnet247.com/247reference/msgs/7/38073.aspx

 
Answer #3    Answered By: Jezza Brown     Answered On: May 27

I noticed that it was actually YOU that asked the same question at aspfriends
(all those moons ago)

Even the great Sir Alex Lowe wrote


String myLabelString;
String myTextBoxString;
Control myControl;
foreach (myControl in Page.Controls)
{
switch (myControl.GetType().ToString())
{
case "System.Web.UI.TextBox" :
myTextBoxtring += ((TextBox)myControl).Text;
break;

case "System.Web.UI.Label" :
myLabelString += ((Label)myControl).Text;
break;

default :
'Do stuff with non Label/Textbox controls  if you want
break;
}
}


which will basicaly do what I suggested to do ... my code avoids the need to
know the type, it simply prints the type.

Yet I cannot get any further than Mark ? I cannot get them to show up unless
they have been added programmatically.

So I take it all the code at the link must refer to controls that have been
added programmatically ????? does it ???
how did you fathom what was going on when you asked the original ?

Or have you (or anyone) been able to list controls that were added from the
toolbox ??? (i.e. in the designer).

 
Answer #4    Answered By: Norman Ray     Answered On: May 27

That's exactly what is happening!!!!!!!!!!

 
Didn't find what you were looking for? Find more on Page.Controls Collection Or get search suggestion and latest updates.




Tagged: