While redirecting from page to page generally i used to pass values as querystring but as you know query string is not a good approach as there are many security concerns and more over its having a maximum size is of 256 Bytes or ie length 2048 characters. So is it a good approach to access variables by setting previous page ie "<%# PreviousPageType VirtualPath="" %>" and accessing previous page items
Please let me know, is there any other way for passing variables other than Sessions and is using Previous page concept a Good Approach?
Here is from MSDN's How to: Pass Values Between ASP.NET Web Pages:
You can pass information between pages in various ways, some of which
depend on how the redirection occurs. The following options are
available even if the source page is in a different ASP.NET Web
application from the target page, or if the source page is not an
ASP.NET Web page:
Use a query string.
Get HTTP POST information from the source page.
The following options are available only when the source and target
pages are in the same ASP.NET Web application.
Use session state.
Create public properties in the source page and access the property values in the target page.
Get control information in the target page from controls in the source page.
So assuming you have the same ASP.NET application PreviousPage property is what exactly was designed for your case.
PreviousPage is intended for use with cross-page postbacks. If that's what you're doing, then there's nothing wrong with it.
Without a postback, a reference to an instance of the previous page class won't (shouldn't) contain any state information.
If you're just using a link from one page to another, it's better to use something like session state, cookies, or your database. If you're doing a redirect, there are a few more options (including the use of Server.Transfer() instead of doing an actual redirect).
You can use a alternative approach, you should keep you data in context.items
This approach is very useful to save data in between two or more sequential pages. Its a simple approach used same as:
Context.Items.Add("SOMETHING","somevalue");
* You need to use **Server.Transfer
instead of Response.Redirect. Because if you use Response.Redirect method, your context would be cleared and you cannot get any values.
Hope this will helps you.
Related
I am trying to make this feature, and I'm really stuck.
I have two applications that run on the same domain. and I need to have one application load pages from the other one inside it's own (the first) master page.
I have full control of the code of both sides, of course.
I have tries using HTTPRequest, and HTTPResponse, and I have tried using WebBrowser. Both work great as long as I have static(plain HTML) pages. However,
those pages are actually dynamic. the user need to press server-side buttons (postback) and generally use the session, viewstate, and/or cookies.
because of that, HTTPRequest and WebBrowser fail me, as they do not cause postback, and therefore those server-side controls are not working. more so, if I try to "fake" a postback by saving the ViewState after each response and than resend it on the next request, after a few (3-4) times the original page will return a "The state information is invalid for this page and might be corrupted" error, even if I use
EnableViewStateMac ="false" EnableSessionState="True" EnableEventValidation ="false" ValidateRequest ="false" ViewStateEncryptionMode ="Never
So... any ideas how can I solve this issue?
Thanks in advance
What is the main desire here?
Wrap one site's content in another without any architecture changes?
ANSWER: Iframe
Have a single submit button submit from two sites?
ANSWER: Not a good idea. You might be able to kludge this by creating a scraper and parser, but it would only be cool as an "I can do it trophy". Better to rearchitect the solution. But assuming you really want to do this, you will have to parse the result from the embedded site and redirect the submit to the new site. That site will then take the values and submit the form to the first site and wait for the result, which it will scrape to give a response to the user. It is actually quite a bit more complex, as you have to parse the HTML DOM (easier if all of the HTML is XHTML compliant, of course) to figure out what to intercept.
Caveat: Any changes to the embedded site can blow up your code, so the persons who maintain the first site must be aware of this artificially created dependency so they don't change anything that might cause problems. Ouch, that sounds brittle! ;-)
Other?
If using an iFrame does not work, then I would look at the business problem and draw up an ideal architecture to solve it, which might mean making the functionality of the embedded site available via a web service for the second site.
I have seen this on some survey websites. What is the C# code they use on the client side to keep the URL same, but when clicking the "Next" button, the same aspx page is maintained
without having any query string;
without any change even a character in the url; and
the grid, the data , the content, the questions keep changing?
Can anyone give a code-wise example how to achieve this?
My main query is how is this done in code-behind to change data of page and maintain same url.
Nothing simpler that a session, maintainted at the server side. Store a "current question number" in session, increment it at each succesfull postback and you have what you ask about.
Another possibility - a cookie which contains "current question number".
Both cookie and session are invisible in the query string of course.
"change data of page and maintain same url." Answer is Server.Transfer.
This method will preserve url.
The Next button may submit a form using the HTTP POST method. The form data may contain the session, question and response data. The site uses that to build a new response. Unlike a GET, a POST does not incorporate data into the URL.
Developers will typically accomplish this task by using AJAX. The basic premise behind it is that only a certain portion of the page (e.g. a grid or content area) will make a server call and retrieve the results (using Javascript). The effect achieved is that there has not been a full post back, which is why you don't see the URL or parameters changing.
It is possible to do this using jQuery, pure Javascript, or Microsoft's UpdatePanel.
oleksii's comment has some good links as well:
That's the AJAX magic. There are many JQuery plugings for this, for
example this one with a live demo. You can also program it easily
using JQuery Get or Post or any other wrapper that use XmlHttpRequest
object.
I have now been working as a web developer for two weeks and have written my first page connected it to database have everything setup the way I want and now my next big hurdle. I want to get to this page from another page. the second page emulates written forms and the first page will have a grid of the submitted forms. Looking through the net I have found Iframes and there are a couple of other options I am still reading up about, but i wanted to pose the question here as well. What is the generally accepted / good practice method for navigating from page to page in asp.net. Going from database to web has been a trip but its one i am enjoying.
Thank you for any suggestions
Response.Redirect("Default1.aspx"):
we want to redirect the request to some plain HTML pages on our server or to some other web server
we don't care about causing additional roundtrips to the server on each request
we do not need to preserve Query String and Form Variables from the original request
we want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to bookmark it if its necessary)
Server.Transfer("Default1.aspx") :
we want to transfer current page request to another .aspx page on the same server
we want to preserve server resources and avoid the unnecessary roundtrips to the server
we want to preserve Query String and Form Variables (optionally)
we don't need to show the real URL where we redirected the request in the users Web Browser
If you wanted to simply redirect the user you could use Response.Redirect(url), this will redirect the user to the specified relative page. For example, if you were in Page1.aspx and wanted to redirect to Page2.aspx you would simply write
Response.Redirect("~/Page2.aspx");
Please keep in mind, this is a very simple approach to redirecting, and information submitted from Page1 to Page2 won't get persisted, so you'll need to either save these in the database, or in the session.
Hope this helps a bit. :)
Edit
Reading your question further; if you wanted to load a form after selecting it in Page1, you would want to somehow pass it through to Page2. The easiest way would be to append it to the query string, and then check if the query string value exists on Page2 loads.
You can navigate to another page using
Response.Transfer("Default2.aspx");
Else you can use
Server.Transfer("Default.aspx")
but it is bulky since it transfer that data of previous page too..
Response.Redirect does the job of navigating from one page to another. Below is good article which explains the correct use of it, hope this helps.
http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
In my ASP.NET Website, I am trying to use Server.Transfer to redirect client to different url(all pages resides in same domain) instead of using Response.Redirect because of performance issue and heavy flickering. But when I use Server.Transfer, all session values are wiped out and getting NullReference error even though if I enable page level EnableSessionState. After several attempt, i tried Context.ReWritepath just to change the url and lost all masterpage contents like menus, headers. Any idea? Any help? Am i doing anything wrong?
This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at
Q316920
The unofficial solution is to set the enableViewStateMac property to True on the page you'll be transferring to, then set it back to False. This records that you want a definitive False value for this property and resolves the bug.
Read More here
I have a bunch of parameters that I need to pass onto a second page via request headers. At first I tried via JS but I found out that that's impossible (please correct me if I'm wrong here).
So now I'm trying to do it in the code-behind (via C#). I want to write a bunch of custom request headers and call Response.Redirect or something similar to redirect user to the new page.
Is this possible? If so what methods do I have to use?
Edit: unfortunately using QS parameters is not an option here as it's out of my control.
Use a Server.Transfer("somepage.aspx?parameter1=value");
There is no client redirect then.
You can try setting the headers and do a Server.Transfer - I believe that will work to - up to you, but using the querystring is a bit more readable to me and doesn't show up in the clients browser.
you need to look at state in .net their are various ways to achive state.. in a stateless environment.
i would put it in the session object on page one.. read it on page 2...
create a session object on code behind page 1
read from session object on page 2.
or if you read the msdn state documenation on request paramters this will show you the options avliable.
JS dont worry about doing tricky stuff with it.. mostly trickey is wrong.