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. Problem with Loading an User Control dynamically [modified]

Problem with Loading an User Control dynamically [modified]

Scheduled Pinned Locked Moved ASP.NET
helpquestion
5 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.
  • L Offline
    L Offline
    lovedotnet
    wrote on last edited by
    #1

    hi, I have a menu in Default.aspx, based on the menu clicked corresponding user control is loaded dynamically using LoadControl(). This User control loaded has a formview control in which a dropdown and a textbox are present. When i set the autopost back property of the dropdown to true and select an item in it. The User control which is loaded is disappearing. I have found that the page_load event of the Defualt.aspx is called. I have used Ajax too..but it didn't help me. Can anyone help me ? thanks, deep.

    modified on Friday, April 25, 2008 12:33 AM

    A M 2 Replies Last reply
    0
    • L lovedotnet

      hi, I have a menu in Default.aspx, based on the menu clicked corresponding user control is loaded dynamically using LoadControl(). This User control loaded has a formview control in which a dropdown and a textbox are present. When i set the autopost back property of the dropdown to true and select an item in it. The User control which is loaded is disappearing. I have found that the page_load event of the Defualt.aspx is called. I have used Ajax too..but it didn't help me. Can anyone help me ? thanks, deep.

      modified on Friday, April 25, 2008 12:33 AM

      A Offline
      A Offline
      AlexeiXX3
      wrote on last edited by
      #2

      Dynamically added controls are not regenerated when the page is posted back If you are adding the controls in the pageload inside an ispostback condition, get it out of that condition, and let the controls be added each time the page is posted back

      Alexei Rodriguez

      L 1 Reply Last reply
      0
      • A AlexeiXX3

        Dynamically added controls are not regenerated when the page is posted back If you are adding the controls in the pageload inside an ispostback condition, get it out of that condition, and let the controls be added each time the page is posted back

        Alexei Rodriguez

        L Offline
        L Offline
        lovedotnet
        wrote on last edited by
        #3

        Hi, Then how can i maintain the state of the dropdown ? i.e if i select a particular value and load the control again.....the state of the drop is lost.... how can i fix this ? Thanks, deep.

        A 1 Reply Last reply
        0
        • L lovedotnet

          Hi, Then how can i maintain the state of the dropdown ? i.e if i select a particular value and load the control again.....the state of the drop is lost.... how can i fix this ? Thanks, deep.

          A Offline
          A Offline
          AlexeiXX3
          wrote on last edited by
          #4

          Can you try to add your control in the Page_Init

          Alexei Rodriguez

          1 Reply Last reply
          0
          • L lovedotnet

            hi, I have a menu in Default.aspx, based on the menu clicked corresponding user control is loaded dynamically using LoadControl(). This User control loaded has a formview control in which a dropdown and a textbox are present. When i set the autopost back property of the dropdown to true and select an item in it. The User control which is loaded is disappearing. I have found that the page_load event of the Defualt.aspx is called. I have used Ajax too..but it didn't help me. Can anyone help me ? thanks, deep.

            modified on Friday, April 25, 2008 12:33 AM

            M Offline
            M Offline
            Mircea Grelus
            wrote on last edited by
            #5

            As AlexeiXX3 mentioned, dynamically generated controls are not recreated after the page postbacks. The thing is that the postback resets the page to it's original state, so any information regarding the dynamically created controls is lost. Why is that? Because the Page class is stateless. The Page recreates child controls based on the tags in the aspx files, so your controls not being present there are not shown. What you need to do is recreate the controls in the OnInit event or any event that fires before PageLoad, or in PageLoad if you do a check to see is the page is posted back. See this article[^] as an example. The thing to keep in mind is that when recreating the controls to assign them the same ID they previously had. This way you will retain the state of the controls. That's because when you postback a page the values of the controls are saved in the viewstate, but when state is restored to the controls, it actually looks for the controls with the coresponding ID's on the page. If it can find them it will restore them. So you don't have to worry about state as long as you recreate your controls correctly.

            Cheers, Mircea "Pay people peanuts and you get monkeys" - David Ogilvy

            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