Solved

User controls dissapear after PostBack

Posted on 2008-10-06
4
726 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

624 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