Solved

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

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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