473,320 Members | 2,112 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,320 software developers and data experts.

foreach ImageButton problem

Hello

how come foreach(ImageButton imgbtn in Page.Controls) doesn't work ?

whats wrong in that statement? what should I do? I need to go through
each ImageButton in the Page, without going through all the other
controls.
thanks

Apr 29 '06 #1
3 1866
That loop goes through all controls on first-level on Page's Controls
collection and tries to cast each and every of them into ImageButton, which
obviously won't work. And it's not recursive so it doesn't actually go
through all the controls.

Example which goes through all controls, and does something for every
TextBox

[C#]
private void LoopTextBoxes (Control parent)
{
foreach (Control c in parent.Controls)
{
TextBox tb = c as TextBox;
if (tb != null)
//Do something with the TextBox

if (c.HasControls())
LoopTextBoxes(c);
}
}

And start it by

LoopTextBoxes(Page);

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hello

how come foreach(ImageButton imgbtn in Page.Controls) doesn't work ?

whats wrong in that statement? what should I do? I need to go through
each ImageButton in the Page, without going through all the other
controls.
thanks

Apr 29 '06 #2
I'm doing

foreach (Control ctrl in Page.Controls)
{
if (ctrl is ImageButton)

..... it doesn't work though, i.e. its not finding the ImageButton..
even though the ImageButtons are in the Page. They are in an "absolute"
position.

what should I do ?

Apr 29 '06 #3
Controls being absolutely positioned does not have impact on this. As I
said, you need to do it in recursive manner. Page.Controls representys only
the top-level controls on the Page and most probably contains only three
controls (2 literal controls and HtmlForm between them). Your ImageButtons
would be child controls of HtmlForm (since they are contained inside < form
runat="server">), and therefore iterating just Page.Controls won't get them.

Pick up my previous code as is, just change TextBox to ImageButton and use
it. It doesn't loop just Page.Controls but goes through all controls on the
page as it first goes through Page's Controls, checks if each and every
control has child controls and if they have, goes through them too (as long
as there are controls, no matter hpw deep the hierarchy)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Varangian" <of****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I'm doing

foreach (Control ctrl in Page.Controls)
{
if (ctrl is ImageButton)

.... it doesn't work though, i.e. its not finding the ImageButton..
even though the ImageButtons are in the Page. They are in an "absolute"
position.

what should I do ?

Apr 29 '06 #4

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

Similar topics

2
by: Chuck Hartman | last post by:
I've been trying to add an ImageButton object to a Calendar table cell, but so far I am unable to handle the Command event from that button in my form's code behind. Below is an example of what I...
5
by: J McD | last post by:
Hi I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this...
4
by: angus | last post by:
Dear all, I know that i can add a button with oncommand event "ImageButton_Command" by using the asp:ImageButton tag in the aspx code. <asp:ImageButton id="imagebutton1" runat="server"...
3
by: Benjamin Gavin | last post by:
Hi all, I recently stumbled upon a bug in the ASP.NET framework handling of ImageButton based postbacks. The issues derives from the fact that IE and Mozilla handle the case of a missing image...
2
by: Shapper | last post by:
Hello, I have an ASP:Image and an ASP:ImageButton where I set Width=100. Asp:ImageButton is resized in Internet Explorer but not in Firefox. Asp:Image is resized both in Internet Explorer...
3
by: jens.buchta | last post by:
Hi! I'm using a DataGrid with a template column to display an Image inside of it. I'm hooking into its OnPrerender-Event to set the ImageURL-Property dynamically. Everything works just fine...
3
by: JuniorProgrammer | last post by:
Am trying to add an Image Button into a Datalist at Design time. When I run it it comes up with error: Server tag not well formed. It works perfectly when I add just an Image Control. Please...
0
by: artich | last post by:
Hi, I'm developing an application for an internet TV browser (Opera for TV) and like MSN TV, the browser interprets an ImageButton that displays a large image as an Image Map because the...
0
by: Tom Edelbrok | last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a weird situation. If I start out with any ASPX page that contains an ImageButton control followed by a TextBox control,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.