Solved

How to dynamically change usercontrols? asp.net, c#, code-behind

Posted on 2015-02-06
3
310 Views
Last Modified: 2015-02-07
I need to learn how to switch out usercontrols programatically.
I have two controls: ppLoggedOutMenu.ascx and ppLoggedInMenu.ascx.

I want to put the code in the page load method. I already have some code that is checking whether a cookie exists so I was hoping to just drop some code in to add the ppLoggedInMenu to the "(If cookie != "" )," portion of the code, but I can't seem to figure it out or find an example of how to do this.

Can anyone help?

Thanks,
MHenry
0
Comment
Question by:MHenry
[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
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 40594786
What I would do is put them both in the grid, and then set 1 to visible.

something like:

<asp:Panel runat="server" id="pnlLoggingContext">
    <uc1: runat="server" id="pnlLoggedout" />
    <uc2: runat="Server" id="pnlLoggedIn" />
</asp:Panel>

in your code behind:

if (LoggedIn())
{
  pnlLoggedout.Visible = false;
 pnlLoggedIn.Visible = true;
}
else
{
  pnlLoggedout.Visible = true;
  pnlLoggedIn.Visible = false;
}
0
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 250 total points
ID: 40595282
Hey check this simple code

UserControls_header3 uh3 = (UserControls_header3)this.LoadControl(header3);
phHeaderControls.Controls.Add(uh3);

Or
check another sample project
http://www.codeproject.com/Articles/59781/Dynamic-Loading-of-ASP-NET-User-Controls
0
 
LVL 7

Author Closing Comment

by:MHenry
ID: 40596089
Hope you guys don't mind me splitting the points but I think both answers are great.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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