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. Clearing Textboxes (Foreach) Not Working

Clearing Textboxes (Foreach) Not Working

Scheduled Pinned Locked Moved ASP.NET
csharpdebuggingquestion
7 Posts 4 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.
  • U Offline
    U Offline
    User 9875787
    wrote on last edited by
    #1

    Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }

    Richard DeemingR C W 3 Replies Last reply
    0
    • U User 9875787

      Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Are your TextBoxes immediate descendants of the page? It's unlikely; you've probably got several layers of controls between them. Try this:

      public void ClearTextBoxes(Control control)
      {
      foreach (Control c in control.Controls)
      {
      var textBox = c as TextBox;
      if (textBox != null)
      {
      textBox.Text = " ";
      }
      else if (c.HasControls)
      {
      ClearTextBoxes(c);
      }
      }
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      U 1 Reply Last reply
      0
      • U User 9875787

        Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }

        C Offline
        C Offline
        cyber_addicted
        wrote on last edited by
        #3

        try this code protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(Page.Controls); } void ClearTextBoxes(ControlCollection control) { foreach (Control ctrl in control) { if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty; ClearInputs(ctrl.Controls); } }

        U 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Are your TextBoxes immediate descendants of the page? It's unlikely; you've probably got several layers of controls between them. Try this:

          public void ClearTextBoxes(Control control)
          {
          foreach (Control c in control.Controls)
          {
          var textBox = c as TextBox;
          if (textBox != null)
          {
          textBox.Text = " ";
          }
          else if (c.HasControls)
          {
          ClearTextBoxes(c);
          }
          }
          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          U Offline
          U Offline
          User 9875787
          wrote on last edited by
          #4

          Thanks!

          1 Reply Last reply
          0
          • C cyber_addicted

            try this code protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(Page.Controls); } void ClearTextBoxes(ControlCollection control) { foreach (Control ctrl in control) { if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty; ClearInputs(ctrl.Controls); } }

            U Offline
            U Offline
            User 9875787
            wrote on last edited by
            #5

            Thanks man

            C 1 Reply Last reply
            0
            • U User 9875787

              Thanks man

              C Offline
              C Offline
              cyber_addicted
              wrote on last edited by
              #6

              your welcome

              1 Reply Last reply
              0
              • U User 9875787

                Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }

                W Offline
                W Offline
                wikizhao
                wrote on last edited by
                #7

                #region Clear all text boxes page value (for server-side controls) /// /// ex:FindTextBox(this); /// /// public void FindTextBox(Control c) { if (c.Controls != null) { foreach (Control x in c.Controls) { if (x is System.Web.UI.WebControls.TextBox) { ((System.Web.UI.WebControls.TextBox)x).Text = string.Empty; } FindTextBox(x); } } } #endregion #region Clear all text boxes page value (for client side controls) /// /// ex:FindHtmlInputText(this); /// /// public void FindHtmlInputText(Control c) { if (c.Controls != null) { foreach (Control x in c.Controls) { if (x is System.Web.UI.HtmlControls.HtmlInputText) { ((System.Web.UI.HtmlControls.HtmlInputText) x).Value = string.Empty; } if (x is System.Web.UI.HtmlControls.HtmlTextArea) { ((System.Web.UI.HtmlControls.HtmlTextArea)x).Value = string.Empty; } FindHtmlInputText(x); } } } #endregion ex:FindHtmlInputText(this); Hope you can help!!!

                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