Solved

User controls dissapear after PostBack

Posted on 2008-10-06
4
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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