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. Placeholder problem

Placeholder problem

Scheduled Pinned Locked Moved ASP.NET
winformscomhelpquestion
7 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.
  • J Offline
    J Offline
    Jon G
    wrote on last edited by
    #1

    I have a place holder, where I added some user controls. Whenever I try to do the following: MyPlaceHolder.Controls.Add(LoadControl("MyUserControl.ascx")) The above line of code is executed from a button click. Whenever I execute this code from the button click, Only one of my user control remains on the page (there should be a second one appended to the page) I have come to the conclusion that one of the following is happening: - the placeholder's control collection is deleted, and recreated - the new control is not created at all Can anyone elaborate on this problem for me? Thanks. Jon G www.Gizmocoder.com

    J 1 Reply Last reply
    0
    • J Jon G

      I have a place holder, where I added some user controls. Whenever I try to do the following: MyPlaceHolder.Controls.Add(LoadControl("MyUserControl.ascx")) The above line of code is executed from a button click. Whenever I execute this code from the button click, Only one of my user control remains on the page (there should be a second one appended to the page) I have come to the conclusion that one of the following is happening: - the placeholder's control collection is deleted, and recreated - the new control is not created at all Can anyone elaborate on this problem for me? Thanks. Jon G www.Gizmocoder.com

      J Offline
      J Offline
      Jesse Squire
      wrote on last edited by
      #2

      Any control that you create dynamically will exist only until it is rendered. When you postback to handle an event, the control tree is recreated. The .NET Framework will create any controls that exist statically on the HTML portion of a page. You are responsible for recreating any controls that you've dynamically created through code. Since you load your user control into your placeholder dynamically, you'll need to track how many were created and reload them during each postback cycle. Hope that helps. :) --Jesse

      J 1 Reply Last reply
      0
      • J Jesse Squire

        Any control that you create dynamically will exist only until it is rendered. When you postback to handle an event, the control tree is recreated. The .NET Framework will create any controls that exist statically on the HTML portion of a page. You are responsible for recreating any controls that you've dynamically created through code. Since you load your user control into your placeholder dynamically, you'll need to track how many were created and reload them during each postback cycle. Hope that helps. :) --Jesse

        J Offline
        J Offline
        Jon G
        wrote on last edited by
        #3

        it helps yes. Is there an easier workaround? The re-creation process is quite simple, considering it is only a number, although the data contained inside the control will be extremely complicated to re-generate. At this point in time, I am ready to junk the idea, and start over from scratch. I cannot see myself coding a class to encompass all the data from my custom control. Jon G www.Gizmocoder.com

        J 1 Reply Last reply
        0
        • J Jon G

          it helps yes. Is there an easier workaround? The re-creation process is quite simple, considering it is only a number, although the data contained inside the control will be extremely complicated to re-generate. At this point in time, I am ready to junk the idea, and start over from scratch. I cannot see myself coding a class to encompass all the data from my custom control. Jon G www.Gizmocoder.com

          J Offline
          J Offline
          Jesse Squire
          wrote on last edited by
          #4

          CAUTION: Longwinded ahead. While your custom control reference is gone from the page, its data still exists in the post data. You have a few different options for recovering it.

          • If you recreate the dynamic controls in the OnInit or OnLoad methods (or related events), with the same ID and site them in the same placeholder, in the same order... the .NET framework will do the matching between the post data and any ASP.NET controls. [Note: so the documentation claims... I've had mixed results with this one.]
          • Have your custom control implement IPostBackDataHandler which, IIRC, has the same requirements as the above.
          • You can recreate the dynamic control with the same ID and site it in the same placeholder, and manually scrape the post data for any controls. For ASP.NET controls using myTextbox.Text = Request.Params[myTextbox.UniqueID] will do the trick.

          Though it may be the "dirtiest" of them, I find that I use the last method most often, as many times I don't know what dynamic controls to create until a postback event has been handled. Based on my preference, my advice would be for your custom control, using the last technique, to scrape the postdata for each composite control in the OnPreRender or Render method. That way, you'll retain the custom control's data, and you're pretty certain that it will have been added to the page by rendering time. Hope that helps a bit. :) --Jesse

          J 1 Reply Last reply
          0
          • J Jesse Squire

            CAUTION: Longwinded ahead. While your custom control reference is gone from the page, its data still exists in the post data. You have a few different options for recovering it.

            • If you recreate the dynamic controls in the OnInit or OnLoad methods (or related events), with the same ID and site them in the same placeholder, in the same order... the .NET framework will do the matching between the post data and any ASP.NET controls. [Note: so the documentation claims... I've had mixed results with this one.]
            • Have your custom control implement IPostBackDataHandler which, IIRC, has the same requirements as the above.
            • You can recreate the dynamic control with the same ID and site it in the same placeholder, and manually scrape the post data for any controls. For ASP.NET controls using myTextbox.Text = Request.Params[myTextbox.UniqueID] will do the trick.

            Though it may be the "dirtiest" of them, I find that I use the last method most often, as many times I don't know what dynamic controls to create until a postback event has been handled. Based on my preference, my advice would be for your custom control, using the last technique, to scrape the postdata for each composite control in the OnPreRender or Render method. That way, you'll retain the custom control's data, and you're pretty certain that it will have been added to the page by rendering time. Hope that helps a bit. :) --Jesse

            J Offline
            J Offline
            Jon G
            wrote on last edited by
            #5

            Thanks for the help. I scratched the idea. It was ridiculous. I am going to have to go talk it over with the bosses, but I'm going to do this in a different format. This dynamic stuff is just simply ridiculous (in this particular case) Thanks again for the help. Jon G www.Gizmocoder.com

            J 1 Reply Last reply
            0
            • J Jon G

              Thanks for the help. I scratched the idea. It was ridiculous. I am going to have to go talk it over with the bosses, but I'm going to do this in a different format. This dynamic stuff is just simply ridiculous (in this particular case) Thanks again for the help. Jon G www.Gizmocoder.com

              J Offline
              J Offline
              Jesse Squire
              wrote on last edited by
              #6

              My pleasure. Sorry to hear that it didn't work out for you. --Jesse

              J 1 Reply Last reply
              0
              • J Jesse Squire

                My pleasure. Sorry to hear that it didn't work out for you. --Jesse

                J Offline
                J Offline
                Jon G
                wrote on last edited by
                #7

                Just an update if your interested. I managed to get it to work great. I created a class file to hold all the data for my custom control. Each time a new control is added and modified, it gets saved into my my class object, and I basically just have a collection of that class object for each control. This method works extremely great, plus I have extremly easy access to all the values entered into the control via the class object. Thanks again for your input on this. Jon G www.Gizmocoder.com

                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