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. Adding Textbox dynamically when the value of Drop down list changes

Adding Textbox dynamically when the value of Drop down list changes

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelp
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.
  • F Offline
    F Offline
    fasttoshiba
    wrote on last edited by
    #1

    Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast

    V N 2 Replies Last reply
    0
    • F fasttoshiba

      Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast

      V Offline
      V Offline
      vaghelabhavesh
      wrote on last edited by
      #2

      First set the AutoPostBack property True for DropDownlist and handle the SelectedIndexChange Event.

      void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
      int txtCount = int.Parse(DropDownList1.SelectedValue)
      for(int i=0;i<;txtcount;i++; )
      {
      TextBox txtBox = new TextBox()
      //Assign id and some customization for each textbox
      ....
      .....
      placeHolder.Controls.Add(txtBox)
      }
      }

      Be careful, there is no Undo Button(Ctrl+Z) in life.

      N 1 Reply Last reply
      0
      • F fasttoshiba

        Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        I suggest you to put the textbox in a template column inside a GridView, and bind the gridview with a datasource which will have necessary rows. If 1 is selected, make a 1 row data source and bind it to the grid. ASP.NET will maintain viewstate for the texboxes along with parent control, GridView.

        Navaneeth How to use google | Ask smart questions

        F 1 Reply Last reply
        0
        • V vaghelabhavesh

          First set the AutoPostBack property True for DropDownlist and handle the SelectedIndexChange Event.

          void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
          {
          int txtCount = int.Parse(DropDownList1.SelectedValue)
          for(int i=0;i<;txtcount;i++; )
          {
          TextBox txtBox = new TextBox()
          //Assign id and some customization for each textbox
          ....
          .....
          placeHolder.Controls.Add(txtBox)
          }
          }

          Be careful, there is no Undo Button(Ctrl+Z) in life.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)

          Navaneeth How to use google | Ask smart questions

          V S 2 Replies Last reply
          0
          • N N a v a n e e t h

            That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)

            Navaneeth How to use google | Ask smart questions

            V Offline
            V Offline
            vaghelabhavesh
            wrote on last edited by
            #5

            Thanks for correcting my mistake.... I haven't try that way so I wasn't sure just give him a hint.

            Be careful, there is no Undo Button(Ctrl+Z) in life.

            N 1 Reply Last reply
            0
            • V vaghelabhavesh

              Thanks for correcting my mistake.... I haven't try that way so I wasn't sure just give him a hint.

              Be careful, there is no Undo Button(Ctrl+Z) in life.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              You are welcome. Happy to help :)

              Navaneeth How to use google | Ask smart questions

              1 Reply Last reply
              0
              • N N a v a n e e t h

                I suggest you to put the textbox in a template column inside a GridView, and bind the gridview with a datasource which will have necessary rows. If 1 is selected, make a 1 row data source and bind it to the grid. ASP.NET will maintain viewstate for the texboxes along with parent control, GridView.

                Navaneeth How to use google | Ask smart questions

                F Offline
                F Offline
                fasttoshiba
                wrote on last edited by
                #7

                That is a good one...but i am also trying to store this data which is entered in textboxes into a database..How can i handle this... Eg: I am working on registration in Hotel: I can select values from drop-down list(From 1 to 8) and dynamically creating the text boxes. I need to enter user information of all the residents. So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically..... My database have name field,address field.. All the members required to enter the names but on same address... How to store these names into database as they are in random number. Thanks,

                N 1 Reply Last reply
                0
                • F fasttoshiba

                  That is a good one...but i am also trying to store this data which is entered in textboxes into a database..How can i handle this... Eg: I am working on registration in Hotel: I can select values from drop-down list(From 1 to 8) and dynamically creating the text boxes. I need to enter user information of all the residents. So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically..... My database have name field,address field.. All the members required to enter the names but on same address... How to store these names into database as they are in random number. Thanks,

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  fasttoshiba wrote:

                  So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically.....

                  Loop through the gridview rows, find each texbox and save value to database.

                  Navaneeth How to use google | Ask smart questions

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)

                    Navaneeth How to use google | Ask smart questions

                    S Offline
                    S Offline
                    Sundar_R
                    wrote on last edited by
                    #9

                    Think textbox doesnt use viewstate for maintaining the text across postbacks Even if the viewstate is false,text will be still maintained during postbacks Correct me if i am wrong.

                    A 1 Reply Last reply
                    0
                    • S Sundar_R

                      Think textbox doesnt use viewstate for maintaining the text across postbacks Even if the viewstate is false,text will be still maintained during postbacks Correct me if i am wrong.

                      A Offline
                      A Offline
                      Abhijit Jana
                      wrote on last edited by
                      #10

                      Sundar_R wrote:

                      Even if the viewstate is false,text will be still maintained during postbacks

                      You can read This [^] article.

                      cheers, Abhijit CodeProject MVP My Recent Article : Exploring Session in ASP.Net

                      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