Solved

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

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

Accepted Solution

by:
Kyle Abrahams earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Hope you guys don't mind me splitting the points but I think both answers are great.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now