Is it possible to dynamically load an .aspx page? - c#

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?

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).

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";
}
}

Related

Access Control of parent page in iframe

I have a asp.net page (redaktionsplan.aspx), which contains a iframe, which also represent a asp.net-page:
<iframe src='test.aspx' height="1000px" width="700px" frameborder="0">
test.aspx contains a control (label1). Now, I want in the OnLoad-Event of the label1 (which is on test.aspx) (in CodeBehind of the label1) access a control which is on redaktionsplan.aspx.
How is this possible?
You cannot access controls of another page on the server.
The two pages are requested by the browser with two separate HTTP GET requests: the first one for redaktionsplan.aspx, then when that page is rendered in the browser, another request is made for test.aspx.
So the code-behind on these two pages runs sequentially (not at the same time) and in the context of two different requests.
If you need to exchange data between two pages, then you can for example:
let the first page store the data in the session, so that during the second request you can access it
implement the functionality on the client side, using javascript

Windows Phone 8 Databound App and back button

I've something that I can't get myself understand. I'm making an app with databound template. I put a textbox on mainpage and a button. when i type something in textbox and press on button it navigates to the listing page and that content comes from web and then if i press on back button and make a new search the results from previous search stays there. how can i reset/clear or disable cache of that page?
It would be helpful if you could post your XAML and code-behind, but I will attempt to make a jab at an answer. Where are you referencing the call to get the data from the web? If it is in the constructor of the page, then that is why the previous search stays there. What is probably happening is the first search constructs the secondary page, does your web call, and binds your data to the page. Then when you press the back button, and click it again, the page is already constructed, so it uses the same data.
It is probably wise to call your web service in the OnNavigatedTo override method. From the first page, you can pass parameters to your secondary page (i.e., pass the search term, then pass the search term into your web service).
Here is an example of passing parameters between pages: http://developer.nokia.com/Community/Wiki/Passing_parameters_while_navigating_between_pages_on_Windows_Phone
Also, make sure your Data Context for the second page is appropriately set each time the page is navigated to, since you have a data bound application.
Without code, I can't really help other than giving these things to think about.

Using Server.Transfer to preserve the page URL, how to keep the URL when clicking a button on the new page?

I'm very very new to asp.net, and in my tour of its features I found that if you use Server.Transfer instead of Response.Redirect then, among other things, you could preserve the URL of the original page. I created two test pages.
The first has a textbox and a button. When you click the button, the contents of the textbox are saved in the Session variable and Server.Transfer is used to load the second page. On this page there's a button and a label. When you click the button, the label gets populated with what was saved in the session variable.
The issue is, when I click the button on the second page and the label is altered, the URL changes to that of the second page. This seems a bit to defeat the purpose, so how do I go about preserving the URL?
Clicking the button on the second page is causing a postback and the server is showing the URL of the page you are posting back to (the second page). In effect, you have done a Response.Redirect to yourself.
I am curious as to why you want to have two separate .aspx pages behave as if they are only one. One of the major drawbacks of using Server.Transfer is the confusion it causes the user when they think they are on a new page, but the browser says otherwise; especially in bookmarking scenarios.
If you want the logic to reside in a single .aspx page, but act as two separate logical units, then I suggest you use ASP.NET Panel controls that show/hide the logic as needed and the page's code-behind can react to the necessary events (i.e. button clicks) all in one page and the URL will be the same the entire time.

How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC?

So, I have a page that looks like the following:
alt text http://img704.imageshack.us/img704/5973/croppercapture3.png
This is my basic messaging inbox. I have 2 pages right now, Inbox and Sent (there will eventually be others). These both share the same View, and just have 2 different actions to populate the appropriate data.
The elements on the right-hand side (the message list) is a partial view which has its data populated based on my two controller actions Inbox(int? page) and Sent(int? Page).
So, I have one view "MessageView" and one partial view "MessageList" shared between Inbox/Sent.
However, I now have to get those arrow buttons ("<" ">") working using Ajax. I know how to use jQueries ajax calls well enough, and I know how to render the result of the action call (which returns a partial view).
The problem comes from the fact that the javascript that makes these pagination ajax calls needs to know two things:
What the current page is (whether it be /messages/inbox or /messages/sent)
What the current page is (specified in the query string, ie /messages/inbox?page=2).
Without knowing which page I'm on (Inbox or Sent), it wont know which url to make the ajax call on. Should it make the postback to /messages/inbox or to /messages/sent?
If I wasn't making these messages load with Ajax it would be as simple as loading the appropriate url into the link tags for the "<" and the ">" buttons. But I can't, because part of my requirements states that it must load the messages below without visibly refreshing to a new page.
In JavaScript you can check window.location.pathname to see the pathname section of the current’s page’s URL.
window.location.search gives you the query string.
When the user clicks the Inbox or Sent buttons, you need to rewrite the URLs in your arrows so that they point to the right place.

Dynamic Pages From a Database in C#

Forgive me if this has already been asked somewhere, but I cannot figure out the best way to accomplish this task. I want to be able to create a rendering system that will allow me to render out content from thousands of different .aspx pages without having to create thousands of .aspx pages. That being said, I still want to be able to render out the appropriate .aspx page if it exists in my code.
For example, when a request is made to the site, I want to check and see if that URL is in the database, if it is, then I want to render the content appropriately. However, if it doesn't, then I want it to continue on to rendering the real .aspx page.
In trying to use an HTTPModule, I cannot get the page that exists in the database to write out the appropriate content. Here's my code.
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
//Checks to see if the page exists in the database
PageInformation page = PageMethods.GetPageFromUrl(url.AbsolutePath);
if (page != null)
{
string renderedPage = Renderer.RenderPage(page);
application.Context.Response.Write(renderedPage);
}
}
However, when trying to use an HTTPHandler, I can't get the real .aspx pages to render appropriately because the *.aspx verb is being dealt with by the handler.
If anyone has any better ideas on how to completely re-design this, I'm completely open to that as well. Thanks.
This will do the trick:
Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
I think you're lookign for a simple URL rewriting example.
So you have a single page "default.aspx" that could take an argument of the content you want to display "default.aspx?page=home", but you don't want the nasty query string part "?page=home".
this is best solved by URL rewriting which can be used as an ISAPI module in IIS. So instead of the URL string above, people see a page called "home.aspx", and the web server translates this into "default.aspx?page=home" for your page which can go get the content for the "home" page out of the DB and display it on the screen.
Here's a page with more information on a good implementation of this process:
http://www.opcode.co.uk/components/rewrite.asp
I believe this shows how to process the "normal" pages inside a handler
other example

Categories

Resources