Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Looping through all controls on a webform

Looping through all controls on a webform

Scheduled Pinned Locked Moved C#
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    caheo
    wrote on last edited by
    #1

    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?

    C D A 3 Replies Last reply
    0
    • C caheo

      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?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • C caheo

        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?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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 your if 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

        H 1 Reply Last reply
        0
        • C Colin Angus Mackay

          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

          C Offline
          C Offline
          caheo
          wrote on last edited by
          #4

          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

          C H 2 Replies Last reply
          0
          • D Dave Kreskowiak

            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 your if 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

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            There you go again, Dave! :rolleyes: GetType is a VB.NET operator equivalent to C#'s typeof operator.

            Microsoft MVP, Visual C# My Articles

            D 1 Reply Last reply
            0
            • C caheo

              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

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C caheo

                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?

                A Offline
                A Offline
                Andy Brummer
                wrote on last edited by
                #7

                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**

                1 Reply Last reply
                0
                • C caheo

                  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

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Are you sure your TextBoxes aren't in other naming containers (like the Panel)? Just because all the TextBoxes 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

                  1 Reply Last reply
                  0
                  • H Heath Stewart

                    There you go again, Dave! :rolleyes: GetType is a VB.NET operator equivalent to C#'s typeof operator.

                    Microsoft MVP, Visual C# My Articles

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    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

                    H 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      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

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #10

                      See what VB.NET is doing to you? Break away now or be doomed... :)

                      Microsoft MVP, Visual C# My Articles

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups