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. Web Development
  3. ASP.NET
  4. Get all TextBoxes on the Page

Get all TextBoxes on the Page

Scheduled Pinned Locked Moved ASP.NET
tutorial
7 Posts 3 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.
  • M Offline
    M Offline
    Marek Konieczny
    wrote on last edited by
    #1

    Hi I would like to access contents controls on a web page using variables instead of calling them directly and actually don't know how to do it. To make it clear: I have a some TextBox controls with ID 1 to 10 and want to access them in a loop and read their contents Thanks

    T 1 Reply Last reply
    0
    • M Marek Konieczny

      Hi I would like to access contents controls on a web page using variables instead of calling them directly and actually don't know how to do it. To make it clear: I have a some TextBox controls with ID 1 to 10 and want to access them in a loop and read their contents Thanks

      T Offline
      T Offline
      TylerBrinks
      wrote on last edited by
      #2

      Recurse through a parent control's collection for text box controls:

      private void ChangeAllTextBoxes(Control parentControl)
      {
      foreach(Control ctl in parentControl)
      {
      if(ctl is TextBox)
      {
      ...
      }
      else if(ctl.HasControls)
      {
      ChangeAllTextBoxes(ctl);
      }
      }
      }

      M 1 Reply Last reply
      0
      • T TylerBrinks

        Recurse through a parent control's collection for text box controls:

        private void ChangeAllTextBoxes(Control parentControl)
        {
        foreach(Control ctl in parentControl)
        {
        if(ctl is TextBox)
        {
        ...
        }
        else if(ctl.HasControls)
        {
        ChangeAllTextBoxes(ctl);
        }
        }
        }

        M Offline
        M Offline
        Marek Konieczny
        wrote on last edited by
        #3

        This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }

        T R 2 Replies Last reply
        0
        • M Marek Konieczny

          This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }

          T Offline
          T Offline
          TylerBrinks
          wrote on last edited by
          #4

          Ok, so you have to account for my typos.. I didn't have a VS IDE open to write it. It should say parentControl.Controls. Use a bit of common sense and at least try a variaion if my code didn't directly compile. As far as your problem goes... be specific. Your posts are too vague to tell what you're asking for.

          M 1 Reply Last reply
          0
          • T TylerBrinks

            Ok, so you have to account for my typos.. I didn't have a VS IDE open to write it. It should say parentControl.Controls. Use a bit of common sense and at least try a variaion if my code didn't directly compile. As far as your problem goes... be specific. Your posts are too vague to tell what you're asking for.

            M Offline
            M Offline
            Marek Konieczny
            wrote on last edited by
            #5

            Thanks, with parentControl.Controls it works. Sorry, there was a little bit late here and my brain did function very slowly ;-) Now I will try to describe my problem: I was wondering if there is possible to define a string which has a control name as it contents eg. fName = "TextBox1" and then read or change the content of this control, e.g Textbox1.Text using fName variable. I hope now I expressed myself clearer :) Marek

            T 1 Reply Last reply
            0
            • M Marek Konieczny

              This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }

              R Offline
              R Offline
              Rhys Gravell
              wrote on last edited by
              #6

              OK. You can do what you want I believe. Starting at the top level of the Page, you need to find the HtmlForm control as thats the top level control to which all controls on a page belong, and then iterate it's child controls being careful to make sure that you test each control you find for child controls and checking them iteratively for children in turn to ensure you pick up all controls and all their iterative children. When you find the bottom level child controls one at a time you then need to check if the control you find is a textbox and do something with it. The below is a top level method looking at all controls on the Page to which it belongs, and processing the HtmlForm control (when found) to iterate it's child controls; foreach(Control c in this.Controls) { if (c is HtmlForm) { HtmlForm iHtmlForm = (HtmlForm)c; foreach(Control iHtmlFormChild in iHtmlForm.Controls) { if (iHtmlFormChild.HasControls()) { IterateControls(iHtmlFormChild); } else if (iHtmlFormChild is TextBox) { TextBox iTextBox = (TextBox)iHtmlFormChild; //Do something with the instance of the TextBox here } } } } You'll notice an IterateControls method here, which checks each child of the HtmlForm for Child controls. This is below; private void IterateControls(Control control) { foreach(Control iChildControl in control.Controls) { if (iChildControl.HasControls()) { IterateControls(iChildControl); } else if (control is TextBox) { TextBox iTextBox = (TextBox)control; //Do something with the instance of the TextBox here } } } And I know this works. Test it by creating a page with a number of TextBox controls on it. Put the first method in the Page_Load event and use the second method as a Private method of the page. Change '//Do something with the instance of the TextBox here' to 'TextBoxID.Text += iTextBox.ID.ToString();' and the Text in the TextBox to which the ID of all other controls is being written should contain all their ID's when the page renders. I'm sure you can take it from there... Rhys A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation... Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die.

              1 Reply Last reply
              0
              • M Marek Konieczny

                Thanks, with parentControl.Controls it works. Sorry, there was a little bit late here and my brain did function very slowly ;-) Now I will try to describe my problem: I was wondering if there is possible to define a string which has a control name as it contents eg. fName = "TextBox1" and then read or change the content of this control, e.g Textbox1.Text using fName variable. I hope now I expressed myself clearer :) Marek

                T Offline
                T Offline
                TylerBrinks
                wrote on last edited by
                #7

                You can set the ID of a control as follows: myControl.ID ="myCtlID"; then use the .FindControl("myCtlID") method in the parent page/control. It'll return a control that you can cast to a text box and do what you need.

                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