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. Dynamically Adding Textboxes

Dynamically Adding Textboxes

Scheduled Pinned Locked Moved ASP.NET
csharpquestion
6 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.
  • A Offline
    A Offline
    Adam Wimsatt
    wrote on last edited by
    #1

    I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick... TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp); The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted]

    C Z 2 Replies Last reply
    0
    • A Adam Wimsatt

      I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick... TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp); The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted]

      C Offline
      C Offline
      ChrisAdams
      wrote on last edited by
      #2

      Each time you call the page, remember that asp.net has to recreate all the control, by parsing the aspx page. So each time it loads it will create a new placeholde object which has 0 child controls, because that it what is in you aspx page. So on each successive post you will need to add code to work out how many text boxes to add, depending on how many times the button has been click. As a suggestion you could store the number of textboxes within a viewstate variable. Hope this points you in the right direction.

      A 1 Reply Last reply
      0
      • A Adam Wimsatt

        I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick... TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp); The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted]

        Z Offline
        Z Offline
        Zee_Zee
        wrote on last edited by
        #3

        I don't know if I got it right but isn't this better to use client side scripting in such scenario? If yes, then simply create those controls using javascript. If no, then please let me know why. Regards, Zee_Zee

        A C 2 Replies Last reply
        0
        • C ChrisAdams

          Each time you call the page, remember that asp.net has to recreate all the control, by parsing the aspx page. So each time it loads it will create a new placeholde object which has 0 child controls, because that it what is in you aspx page. So on each successive post you will need to add code to work out how many text boxes to add, depending on how many times the button has been click. As a suggestion you could store the number of textboxes within a viewstate variable. Hope this points you in the right direction.

          A Offline
          A Offline
          Adam Wimsatt
          wrote on last edited by
          #4

          Thanks for pointing me in the right direction. It works good!

          1 Reply Last reply
          0
          • Z Zee_Zee

            I don't know if I got it right but isn't this better to use client side scripting in such scenario? If yes, then simply create those controls using javascript. If no, then please let me know why. Regards, Zee_Zee

            A Offline
            A Offline
            Adam Wimsatt
            wrote on last edited by
            #5

            I want the controls to runat="server" and i don't think adding them via client side will work.

            1 Reply Last reply
            0
            • Z Zee_Zee

              I don't know if I got it right but isn't this better to use client side scripting in such scenario? If yes, then simply create those controls using javascript. If no, then please let me know why. Regards, Zee_Zee

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

              Depends on what he's trying to do in the long run. I don't think we have enough information either way, but if they are created on the client side using javascript then they will not be part of the ASP.NET control structure and not able to participate in the ViewState, or be accessed from the asp.net as a control. Unless I'm wrong, which I could be...

              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