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. Values from Textbox

Values from Textbox

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesign
5 Posts 2 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.
  • D Offline
    D Offline
    danasegaranea
    wrote on last edited by
    #1

    Hi all, I am using this code to create textbox in asp.net pages and adding it to the panel control Page load Event

    TextBox txtbox;
    for (int i = 1; i < 10; i++)
    {
    txtbox = new TextBox();
    txtbox.ID = "mytextbox" + i;
    txtbox.AutoPostBack = false;
    Panel1.Controls.Add(txtbox);
    }

    ViewSource <div id="Panel1"> <input name="mytextbox1" type="text" id="mytextbox1" /><input name="mytextbox2" type="text" id="mytextbox2" /><input name="mytextbox3" type="text" id="mytextbox3" /><input name="mytextbox4" type="text" id="mytextbox4" /><input name="mytextbox5" type="text" id="mytextbox5" /><input name="mytextbox6" type="text" id="mytextbox6" /><input name="mytextbox7" type="text" id="mytextbox7" /><input name="mytextbox8" type="text" id="mytextbox8" /><input name="mytextbox9" type="text" id="mytextbox9" /> </div> and want to retrive the textbox values in a button click event. But it not iterarting throgh the textbox collection

    protected void Button2_Click(object sender, EventArgs e)
    {

        IterateThroughChildren(this);
    }
    
    void IterateThroughChildren(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
                  && c.ID == null)
            {
                TextBox txtbox = (TextBox)c;
            }
    
            if (c.Controls.Count > 0)
            {
                IterateThroughChildren(c);
            }
        }
    }
    

    Thanks in Advance

    N 1 Reply Last reply
    0
    • D danasegaranea

      Hi all, I am using this code to create textbox in asp.net pages and adding it to the panel control Page load Event

      TextBox txtbox;
      for (int i = 1; i < 10; i++)
      {
      txtbox = new TextBox();
      txtbox.ID = "mytextbox" + i;
      txtbox.AutoPostBack = false;
      Panel1.Controls.Add(txtbox);
      }

      ViewSource <div id="Panel1"> <input name="mytextbox1" type="text" id="mytextbox1" /><input name="mytextbox2" type="text" id="mytextbox2" /><input name="mytextbox3" type="text" id="mytextbox3" /><input name="mytextbox4" type="text" id="mytextbox4" /><input name="mytextbox5" type="text" id="mytextbox5" /><input name="mytextbox6" type="text" id="mytextbox6" /><input name="mytextbox7" type="text" id="mytextbox7" /><input name="mytextbox8" type="text" id="mytextbox8" /><input name="mytextbox9" type="text" id="mytextbox9" /> </div> and want to retrive the textbox values in a button click event. But it not iterarting throgh the textbox collection

      protected void Button2_Click(object sender, EventArgs e)
      {

          IterateThroughChildren(this);
      }
      
      void IterateThroughChildren(Control parent)
      {
          foreach (Control c in parent.Controls)
          {
              if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
                    && c.ID == null)
              {
                  TextBox txtbox = (TextBox)c;
              }
      
              if (c.Controls.Count > 0)
              {
                  IterateThroughChildren(c);
              }
          }
      }
      

      Thanks in Advance

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      I think you have a bit of a logic error, c.ID will never be null. You also have an unnecessary recursion with the if(c.Controls.Count > 0) All you need is foreach(TextBox txt in Panel1.Controls) { }


      only two letters away from being an asset

      D 1 Reply Last reply
      0
      • N Not Active

        I think you have a bit of a logic error, c.ID will never be null. You also have an unnecessary recursion with the if(c.Controls.Count > 0) All you need is foreach(TextBox txt in Panel1.Controls) { }


        only two letters away from being an asset

        D Offline
        D Offline
        danasegaranea
        wrote on last edited by
        #3

        I got the error as

        Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

        N 1 Reply Last reply
        0
        • D danasegaranea

          I got the error as

          Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          TextBox txtbox = (TextBox)c; You are trying to cast every control in the collection to a TextBox, including the LIteralControl, of course you will get this error. A Literal is not a TextBox :rolleyes: foreach(TextBox txt in Panel1.Controls) This only gets you the TextBox controls in the collection


          only two letters away from being an asset

          D 1 Reply Last reply
          0
          • N Not Active

            TextBox txtbox = (TextBox)c; You are trying to cast every control in the collection to a TextBox, including the LIteralControl, of course you will get this error. A Literal is not a TextBox :rolleyes: foreach(TextBox txt in Panel1.Controls) This only gets you the TextBox controls in the collection


            only two letters away from being an asset

            D Offline
            D Offline
            danasegaranea
            wrote on last edited by
            #5

            It is throwing the error in the previous line itself :omg:

            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