RSS
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

[ASP.NET] [C#] Switching MasterPage Dynamically

Have you ever wanted to changed a MasterPage of an ASPX webpage dynamically when your website is running? Well, I’ve recently encountered this problem myself too and found a solution to it. So without further ado, I present the solution to you!

First go to your code-behind page of the .aspx page which you wish to have its MasterPage changed dynamically. Let’s say for example, you want your example01.aspx to change its MasterPage dynamically, you have to open the example01.aspx.cs page.

Ok here’s the step-by-step walkthrough.

Step 01:

Enter the following code below at anywhere in the public partial class section

Code:
protected void Page_PreInit(object sender, EventsArgs e)
{

}

Explanation:

The MasterPage needs to be defined before the .aspx page is initialized. In simpler terms, without the MasterPage predefined before the .aspx initialized, the .aspx page have no where to be loaded to, because webpages of website which utilizes MasterPage normally and most certainly requires a ContentPlaceHolder for it to be loaded into.

 

Step 02:

Enter the following code in the Page_PreInit body

Code:

MasterPageFile = “YourMasterPage.Master”;

 

Step 03 (optional):

Incorporate conditions with the code. What’s the point of changing MasterPage dynamically if there’s no conditions in the first place, right? The fact that we want to dynamically switch MasterPages, there has to be a condition. Therefore, incorporate them!

In my case, I determine  the user type. If the user who logs in is an admin for example, I will display a MasterPage for admins, if its a user, I’ll display a MasterPage for users.

The code should look something like this:

protected void Page_PreInit(object sender, EventArgs e)
{
     string redirect_from = ((string)Request.QueryString["redirectedfrom"]);

     if (redirect_from == "staff")
     {
          MasterPageFile = "~/MasterPage/StaffMaster.master";
    }
    else
    {
          MasterPageFile = "~/MasterPage/AdminMaster.Master";
    }
}

 

The code above shows the way I incorporated conditions to the code which triggers it to switch MasterPage if a certain user type logs in. If you’re wondering what’s with the “~/MasterPage/”, it is because I stored all my pages in their respective folders and my MasterPages are stored in a folder named MasterPage.

  • www.tips-fb.com
  • Follow LBenjamin on Twitter
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

[ASP.NET][C#] ASP page loading twice

Reason behind this post is because I would like to document all the problems I had encountered and the solution for my and all visitor's convenience in the possible future.

As the titled states; I've recently, to be exact, 2 days back, encountered this strange phenomenon whereby my asp page loads twice when I click on the button, asp:imageButton to be exact. I'm working with this search function you see. I have to pass a value from this page, back to this page. Don't get it?

Let's say, I have a webpage called report.aspx. There's a search function inside, and after the user entered a value, the user has to click on a button to pass that value for a search, right? yea, so, I stored the value using Session, as I thought it was the most convenient and efficient way to pass a value from report.aspx back to report.aspx because the page has to be posted back in order to fire off the next function.

However, as convenient as it may seemed to be, the downside of Session is that it stores in two places at the same time; the server side and the client side. Hence, the page loads twice.

Advance explanation: the first click of the button stores the Session on the Server side but not the client side. Therefore, it only updates the Server side and not the Client side. Which means, the user has to click on the button twice then only the user could view the updated page with the search result.

Solution:
Dispose the use of Session and replace it with QueryString. Problem solved.
I'll assume that everyone who are reading this knows how to work a QueryString. If you have any inquiries or need a pointer, feel free to drop me a comment and I'll get back to you. Cheers all!!

  • www.tips-fb.com
  • Follow LBenjamin on Twitter
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Freelance Programming

I've been wanting to start on freelancing but I'm always so caught up with my life.
Since now I'm thinking about it, I thought "Hey, why not just post it up in my blog to start off".
So here I am, ready to code away!!
I'm an experienced C#.NET and VB.NET programmer.
I do websites too in plain old HTML or even in ASP.NET, although I must admit I'm not that proficient in web-development.

Just send me an email stating the:
- program's objective
- functionality
- and not forgetting, the due date.

and we'll discuss more from there.

My Contacts:
Email: prospectv15@gmail.com
MSN: prospectv15@hotmail.com
Skype: LBenjamin87

  • www.tips-fb.com
  • Follow LBenjamin on Twitter
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS