Looping through all controls on a webform
-
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls) { if(ctr is TextBox) { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Would you please tell me why it does not work?
-
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls) { if(ctr is TextBox) { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Would you please tell me why it does not work?
caheo wrote: Would you please tell me why it does not work? What error do you get? Does it just not update the colours? or does it throw an exception? or just does not compile? That information is usually an excellent start to figuring out why something does not work.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
-
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls) { if(ctr is TextBox) { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Would you please tell me why it does not work?
Since you don't say what happens or what doesn't happen or give any kind of error message (other than "it's broke"), it's difficult to say what you're problem is. I can take a guess and say that you might be having trouble with the
if
statement, so how about making yourif
statement look like this://foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr.GetType().Equals(GetType(TextBox)))
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
caheo wrote: Would you please tell me why it does not work? What error do you get? Does it just not update the colours? or does it throw an exception? or just does not compile? That information is usually an excellent start to figuring out why something does not work.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
It does not give any error. The reason I ask is there are quite a few TextBoxes on the GUI, after doing comparision at the if statement it skips and do the next iteration. Seems to me the if statement never reaches. foreach(Control ctr in Page.Controls) { if(ctr is TextBox) <- it does comparision but never gets inside the if-statement { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Regards
-
Since you don't say what happens or what doesn't happen or give any kind of error message (other than "it's broke"), it's difficult to say what you're problem is. I can take a guess and say that you might be having trouble with the
if
statement, so how about making yourif
statement look like this://foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr.GetType().Equals(GetType(TextBox)))
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
There you go again, Dave! :rolleyes:
GetType
is a VB.NET operator equivalent to C#'stypeof
operator.Microsoft MVP, Visual C# My Articles
-
It does not give any error. The reason I ask is there are quite a few TextBoxes on the GUI, after doing comparision at the if statement it skips and do the next iteration. Seems to me the if statement never reaches. foreach(Control ctr in Page.Controls) { if(ctr is TextBox) <- it does comparision but never gets inside the if-statement { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Regards
Personally I would have written it like this:
foreach(Control ctr in Page.Controls)
{
TextBox t = ctr as TextBox;
if(t != null)
{
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
-
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls) { if(ctr is TextBox) { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Would you please tell me why it does not work?
You aren't looping through **all the controls on the form, just the direct children of the Page. To get all, you would need to recursively search through the control. Also make sure you are doing this in the PreRender event so you can make sure that all the controls have been created.
The architect has placed his bets, but the odds are long -Poster Children**
-
It does not give any error. The reason I ask is there are quite a few TextBoxes on the GUI, after doing comparision at the if statement it skips and do the next iteration. Seems to me the if statement never reaches. foreach(Control ctr in Page.Controls) { if(ctr is TextBox) <- it does comparision but never gets inside the if-statement { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue; t.ReadOnly = false; } } Regards
Are you sure your
TextBox
es aren't in other naming containers (like thePanel
)? Just because all theTextBox
es appear to be in the page, doesn't mean they're direct children of the page. You need to use a recursive methods like so:private void SetReadOnly(Control parent, bool readOnly)
{
if (c != null)
{
foreach (Control child in parent.Controls)
{
if (child is TextBox)
{
TextBox tb = (TextBox)tb;
tb.BackColor = Color.AliceBlue;
tb.ReadOnly = readOnly;
}
// Recursively call with child controls' Controls
SetReadOnly(child, readOnly);
}
}
}To call this, just pass the
Page
reference to this property:SetReadOnly(Page, false);
For more information, see the
INamingContainer
interface documentation in the .NET Framework SDK.Microsoft MVP, Visual C# My Articles
-
There you go again, Dave! :rolleyes:
GetType
is a VB.NET operator equivalent to C#'stypeof
operator.Microsoft MVP, Visual C# My Articles
DAMN! :mad: I can't keep anything straight nowadays! :doh: :rolleyes: Now where did my mouse go...:doh: RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
DAMN! :mad: I can't keep anything straight nowadays! :doh: :rolleyes: Now where did my mouse go...:doh: RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
See what VB.NET is doing to you? Break away now or be doomed... :)
Microsoft MVP, Visual C# My Articles