Solved

User controls dissapear after PostBack

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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