Solved

User controls dissapear after PostBack

Posted on 2008-10-06
4
713 Views
Last Modified: 2013-12-17
Hi Experts.

I need a bit of advice here please.
I have a Panel control on my web page.
Depending on certain conditions I have various user controls that suppose to show up in that Panel.

For example if button A is clicked I execute mainPanel.Controls.Add(userControl1);
If button B is clicked I execute manlPanel.Controls.Add(userControl2);

All user controls have their own "Submit" buttons.

The problem is that User Controls disappear from the Panel once the Submit button is pressed and it does not do what the control is suppose to be doing.

I am aware that the page is completely refreshed during the postback causing my user controls disappear from the panel.
How would you handle/ resolve this issue though ? I can't just bind user controls to the Panel on the Page_Load event as I have to determine which control to load based on the Button selected on my Web Page.

Thank you in advance and let me know if any of this isn't clear.
0
Comment
Question by:techsuppoprt
4 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 22652756
I suppose the problem is that you add user control to panel only once and do not do this after postback.

If I were you I'd add all user contols into panel and control their visibility instead of adding them to panel on the same conditions.
If button A is pressed set all user controls Visible = false and userControl1.Visible = true.

Just that simple

R
0
 
LVL 7

Accepted Solution

by:
imateyelectronics earned 500 total points
ID: 22652832
Is passing the information back to the page via a hidden field out of the question?
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22653190
Controls must be re-added for each postback.

http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

When the page first gets a button click store the required control name in viewstate, on subsequent postbacks check this to see what control needs re-adding to the control tree, i suggest you do this in CreateChildControls on OnInit.
0
 
LVL 1

Author Closing Comment

by:techsuppoprt
ID: 31503529
QUick and simple.. don't know how I didn't think of that.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now