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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET 2.0 - Adding controls at runtime?

ASP.NET 2.0 - Adding controls at runtime?

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netquestion
9 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.
  • W Offline
    W Offline
    wbjohnson
    wrote on last edited by
    #1

    Hey guys i have a small problem.... im sure you can help I have a new project, with 2 buttons on Button1 and Button2... The code behind button1 is simple: Textbox t = new Textbox(); t.Text = "hello world!"; form1.Controls.Add(t); when i click Button1 - wow! it adds the control! No problem, i noticed that i can click the button as many times as i want and only 1 textbox ever appears. so Click button1, then click Button2 - POW! its vanised! Whats the correct method of keeping a control "alive" on a page (as well as preserving any events it has?) Cheers Will

    M G 2 Replies Last reply
    0
    • W wbjohnson

      Hey guys i have a small problem.... im sure you can help I have a new project, with 2 buttons on Button1 and Button2... The code behind button1 is simple: Textbox t = new Textbox(); t.Text = "hello world!"; form1.Controls.Add(t); when i click Button1 - wow! it adds the control! No problem, i noticed that i can click the button as many times as i want and only 1 textbox ever appears. so Click button1, then click Button2 - POW! its vanised! Whats the correct method of keeping a control "alive" on a page (as well as preserving any events it has?) Cheers Will

      M Offline
      M Offline
      Member 96
      wrote on last edited by
      #2

      I'm not an expert by any means on this, however I believe it might have to do with the point at the page event lifecycle when you create the controls. From what I understand any dynamic controls should be created very early in the page lifecycle and I use the Page_Init event always for dynamic controls and have had no problems with them. However I did have problems when trying to create them on page_load.

      G 1 Reply Last reply
      0
      • W wbjohnson

        Hey guys i have a small problem.... im sure you can help I have a new project, with 2 buttons on Button1 and Button2... The code behind button1 is simple: Textbox t = new Textbox(); t.Text = "hello world!"; form1.Controls.Add(t); when i click Button1 - wow! it adds the control! No problem, i noticed that i can click the button as many times as i want and only 1 textbox ever appears. so Click button1, then click Button2 - POW! its vanised! Whats the correct method of keeping a control "alive" on a page (as well as preserving any events it has?) Cheers Will

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Every postback produces a completely new Page object, so to make a dynamically created control stay on the page, you have to recreate it on every postback.

        --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • M Member 96

          I'm not an expert by any means on this, however I believe it might have to do with the point at the page event lifecycle when you create the controls. From what I understand any dynamic controls should be created very early in the page lifecycle and I use the Page_Init event always for dynamic controls and have had no problems with them. However I did have problems when trying to create them on page_load.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You shouldn't have any problem with adding controls in Page_Load. The Page_Init didn't event exist in framework 1.x.

          --- b { font-weight: normal; }

          M 1 Reply Last reply
          0
          • G Guffa

            You shouldn't have any problem with adding controls in Page_Load. The Page_Init didn't event exist in framework 1.x.

            --- b { font-weight: normal; }

            M Offline
            M Offline
            Member 96
            wrote on last edited by
            #5

            Right, although they do recommend Page_Init and some 3rd party components I'm using require that as well because if you create them any later you don't get their values properly from viewstate. http://support.microsoft.com/kb/317515

            G 1 Reply Last reply
            0
            • M Member 96

              Right, although they do recommend Page_Init and some 3rd party components I'm using require that as well because if you create them any later you don't get their values properly from viewstate. http://support.microsoft.com/kb/317515

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Yes, for some reason that is not revealed, it says that Page_Init is the best place for it, but it also says: "When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler." So, either works for dynamic controls. If some controls need to be created in Page_Init, it's because how they are built, not because you create them dynamically.

              --- b { font-weight: normal; }

              M 1 Reply Last reply
              0
              • G Guffa

                Yes, for some reason that is not revealed, it says that Page_Init is the best place for it, but it also says: "When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler." So, either works for dynamic controls. If some controls need to be created in Page_Init, it's because how they are built, not because you create them dynamically.

                --- b { font-weight: normal; }

                M Offline
                M Offline
                Member 96
                wrote on last edited by
                #7

                No, I looked a little deeper into it and you *must* create them in Page_Init if you want their viewstate to be restored properly because the event that loads viewstate into the controls occurs before Page_Load but after Page_Init.

                G 1 Reply Last reply
                0
                • M Member 96

                  No, I looked a little deeper into it and you *must* create them in Page_Init if you want their viewstate to be restored properly because the event that loads viewstate into the controls occurs before Page_Load but after Page_Init.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Are you sure? Has this changed between framework 1.1 and framework 2.0? Otherwise it would be impossible to use dynamic controls in framework 1.1, as there is no Page_Init.

                  --- b { font-weight: normal; }

                  M 1 Reply Last reply
                  0
                  • G Guffa

                    Are you sure? Has this changed between framework 1.1 and framework 2.0? Otherwise it would be impossible to use dynamic controls in framework 1.1, as there is no Page_Init.

                    --- b { font-weight: normal; }

                    M Offline
                    M Offline
                    Member 96
                    wrote on last edited by
                    #9

                    Maybe it's changed, not sure, I never used .net 1.x with asp.net, but that's been my experience with asp.net 2.0 and that's what Microsoft detailed on the page I found at the time when I looked it up on MSDN. I use dynamic controls for a custom fields section on many of my forms and if I don't create them in page init then I can't retrieve their values later on in the page lifecycle.

                    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