Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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