Solved

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

Posted on 2015-02-06
3
295 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

23 Experts available now in Live!

Get 1:1 Help Now