Is it possible to dynamically load an .aspx page?

I received a new requirement today: For our product page, they want a completely different layout to be used based on the product type.

For example, say we sell buckets. Currently, all buckets use the exact same page layout. But now, they want wooden buckets to use the current layout, and plastic buckets to use a completely different layout. However, they want the URLs to stay the same (e.g., domain.com/bucket/1), so I can’t just forward plastic buckets to a new page.

The current page structure is as follows:

CurrentMasterPage.master > CurrentProductPage.aspx > Several UserControls

The new layout requires new pages (i.e., none of the current ones are reused):
NewMasterPage.master > NewProductPage.aspx > Several UserControls

My first thought was to take all of the markup and code from CurrentProductPage.aspx and put it into a UserControl, then create a second UserControl for the new layout (NewProductPage.aspx), and have CurrentProductPage.aspx dynamically load the appropriate UserControl based on the product type, however, this doesn’t work because the new layout requires a new MasterPage, and I can’t reference a MasterPage from a UserControl.

Then, I thought about using URL Rewriting, but I don’t think it’s possible to have the same URL load two different pages.

Is there any way to accomplish this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Why not use a 100% server side re-direct?

When you use response.Redirect(“some different page”). Then the client side browser is sent a whole new copy of that page, and the URL will be updated.

However, the server side can write any page it wants to. The client side will not even know the server decided to dish out a different page for the given URL.

So, you could have a page with fake tabs as buttons. When the user hits a button, the browser round trip starts (for the given URL). But on server side, you can then dish out a different page for that URL.

So, in place of this classic “round trip”, you can use:

Server.TransferRequest("MyotherWebPage")

So, for the given URL, before the current page (based on given URL) is sent down back to the browser, the above will simply pump out a different page. The current page will never make it back down to the browser.

In fact for a rich page with lots of buttons and features, you can change the page displayed. So in on-load – simply in place of a “response.Redirect”, use a server.Transfer. The current page never makes it to the client – the one you dish out where. Because the client side has zero clue about what the web server decides to dish out – it will also have zero clue that a different page was send back to the client.

Try the above with a test page.

On page A, behind a standard button, jump to web page B

eg:

Response.Recdirect("MyPageB.aspx")

Note the URL change – classic round trip.

Now, do this with the button:

Server.Redirect("MyPageB.aspx")

In this case, no full round trip occurs. The server transfers directly to the new page and sends that out. (and note how your URL does NOT change).

Method 2

You can change the Master Page on PreInit on the Page using a Master. This is possible because a Master is basically the same as a User Control and is loaded AFTER the page’s code behind.

protected void Page_PreInit(object sender, EventArgs e)
{
    if (NewProductPage)
    {
        MasterPageFile = "~/NewMasterPage.master";
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x