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. how to dynamically add textboxes

how to dynamically add textboxes

Scheduled Pinned Locked Moved ASP.NET
helptutorial
3 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
    MalarGayu
    wrote on last edited by
    #1

    hi friends i need to know how to dynamically add textboxes on the click of a button say: if i click the button once 1 textbox should be added if i click the button second time another textbox should be added and so on... so how to do it... any help K.Gayathri

    U M 2 Replies Last reply
    0
    • M MalarGayu

      hi friends i need to know how to dynamically add textboxes on the click of a button say: if i click the button once 1 textbox should be added if i click the button second time another textbox should be added and so on... so how to do it... any help K.Gayathri

      U Offline
      U Offline
      uspatel
      wrote on last edited by
      #2

      public partial class newTemplate : Form
      {
      int i = 0;
      int x = 22;
      int noofcontrols = 0;
      TextBox[] t1 = new TextBox[25];
      Label[] l1 = new Label[25];

      private void button2_Click(object sender, EventArgs e)
      {
      //Create textboxes for each value
      t1[i] = new TextBox();
      l1[i] = new Label();
      t1[i].Size = new System.Drawing.Size(244, 22);
      t1[i].Location = new System.Drawing.Point(85, x);
      l1[i].Location = new System.Drawing.Point(60, x);
      l1[i].Font =new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      l1[i].Text = i.ToString()+". ";

              t1\[i\].TabIndex = i;
              t1\[i\].Name = "txt"+i;
             
              panel1.Controls.Add(t1\[i\]);
              panel1.Controls.Add(l1\[i\]);
              panel1.AutoSize = true;
              panel1.Show();
              panel1.Refresh();
              t1\[i\].Focus();
              i++;
              x = x +30 ;
              noofcontrols++;
              
      
          }
      

      }

      1 Reply Last reply
      0
      • M MalarGayu

        hi friends i need to know how to dynamically add textboxes on the click of a button say: if i click the button once 1 textbox should be added if i click the button second time another textbox should be added and so on... so how to do it... any help K.Gayathri

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        Seeing that this is posted under asp.net i will give you a client side solution in "jQuery":

        function GenerateTextBoxes( sender )
        {
        var txtBoxHtml = '';
        sender.append( txtBoxHtml );//this will append a new textbox to the already existing ones
        }

        To make it easier for you, i added a css class (text-boxes) that you can later use to retrieve the values of your textboxes if need be and below is how you can achieve that using "jQuery":

        function GetTextBoxValues()
        {
        var arrayOfValues = [];
        $( '.text-boxes' ).each( function(){//loop through all the textboxes with this class names
        if( $(this).val() != '' )
        {
        arrayOfValues.push( $(this).val() ); //and add the values to an array
        }
        });
        }

        Hope that helps, Morgs

        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