Solved

Want to avoid user control to reload its parent page

Posted on 2009-05-15
8
461 Views
Last Modified: 2012-05-07
I have a simple C# page (web form) on which is a user control that is dynamically added to the page - this is a stripped out part of the full application.
When I click on the button on the user control (uc.ascx), instead of running the code in the user control's code behind (which I need it to), it appears to re-load the "parent" page (parent.aspx).
The trouble is, that as the user control is initially added dynamically, through an action of the user, it then disappears from the page. Relevant code copied below:
parent.aspx:
 

    <form id="form1" runat="server">

            <asp:Button ID="btn" Text="DO IT" runat="server" onclick="btn_Click" />

            <div id="div_userControl" runat="server"></div>

    </form>
 

parent.aspx.cs:
 

        protected void Page_Load(object sender, EventArgs e)

        {

            // because of the nature of the application, I can't add the controls here

            // as the control loaded is normally one of several, depending on the situation

            // and I therefore, just use a btn_Click as an example, and to simplify matters!

         }
 

        protected void btn_Click(object sender, EventArgs e)   // run when the button is clicked

        {

            Control template2 = (Control)LoadControl("/controls/uc.ascx");  // load the user control

            div_userControl.Controls.Add(template2);  // add to the dive

        }
 

uc.ascx:
 

    <asp:Button ID="btn" Text="GO" runat="server" onclick="btn_Click" />  // This appears  ok

    <asp:Label ID="lbl" runat="server"></asp:Label>
 

uc.ascx.cs:
 

        protected void Page_Load(object sender, EventArgs e)

        {

        }
 

        protected void btn_Click(object sender, EventArgs e)

        {

            lbl.Text = lbl.Text + "DONE";     // This never gets run, instead parent.aspx gets re-loaded

        }

Open in new window

0
Comment
Question by:SimonGoodchild
  • 5
  • 3
8 Comments
 
LVL 13

Expert Comment

by:iHadi
Comment Utility
Hi

Dynamic added content should always be reloaded or re-added when a GET or POST is sent to the page.

In your situation you have two options: either use querystring , viewstate or javascript to set the type of control to load on page load. Or use a session variable to store the loaded control for future action.

eg: the first time the page is accessed the Session["mycontrol"] == null

the used clicks on the button, you load the control, add it to the page, you should also add it to the session: Session["mycontrol"] == ctrl;

when the page initializes always check Session["mycontrol"], if its not null, just get the control in it and add it to the page. The page will automatically fill it with the new values or respond to its events.
0
 

Author Comment

by:SimonGoodchild
Comment Utility
Thanks, sounds good, however if on my user control I have entered some data (eg. In a text box) I was going to save that entered data to a database. Depending on which user control was loaded, the data to save will always be different so was hoping that I could deal with the saving on the user controls code.   With what you suggest I would lose any entered data when they submit the user controls form. It would work if I was just displaying data on the user control.

0
 

Author Comment

by:SimonGoodchild
Comment Utility
Sorry, just re-read your post, are you saying the user control would remember the values in, say, a textbox? If so, still unsure where I would then save the contents of that textbox to a database. Thanks  
0
 

Author Comment

by:SimonGoodchild
Comment Utility
Ok - I've just got back and tried it out. It looks like the user control does remember what was entered into a textbox for example, and the code behind the user control is run as required. I'm going to try it in the full solution where it wasn't working and will post here afterwards. Fingers crossed, and thanks for the idea!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:SimonGoodchild
Comment Utility
I've added the idea and it looks like it will work - thank you very much indeed!!! Every day is a school day :)
0
 

Author Comment

by:SimonGoodchild
Comment Utility
Solution found, thank you!
0
 
LVL 13

Expert Comment

by:iHadi
Comment Utility
you're welcome
0
 
LVL 13

Accepted Solution

by:
iHadi earned 250 total points
Comment Utility
I just noticed that you chose to close the question without marking my post as a solution.

didn't my solution help you?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

12 Experts available now in Live!

Get 1:1 Help Now