Problem with Loading an User Control dynamically [modified]
-
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
-
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
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
-
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
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.
-
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.
-
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
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 theOnInit
event or any event that fires beforePageLoad
, 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