What technology allows page content to change without changing the URL? - c#

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.

Related

Tips for page navigation for a new web programmer in asp.net

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

Emulating ASP.NET PostBack

I have a crawler application which should parse all items from a Page with paging. Unfortunately the web site that my application crawls uses postbacks for paging. How can I get contents of second page programmatically for following URL in C#.
http://www.hurriyetemlak.com/coldwell-banker-dikey-gayrimenkul/konut-satilik/istanbul-basaksehir-bahcesehir/emlak-ofisleri-ilanlari/3OWB4lkhYFs=/9wZEBZ-ivFgmrA3ENMCIfQ==/qh.BgsUoTK4=/GmMGgVD5Wcc=/GmMGgVD5Wcc=?sParam=3OWB4lkhYFs%3d&sType=9wZEBZ-ivFgmrA3ENMCIfQ%3d%3d&ListIsBig=qh.BgsUoTK4%3d&sortType=GmMGgVD5Wcc%3d&pageSize=GmMGgVD5Wcc%3d
I've tried posting __EVENTTARGET hidden field along __VIEWSTATE and __EVENTVALIDATION but it didn't seem to work.
You can achieve this using screen scraping techniques (See HtmlAgilityPack). This will require you to read the response and reissue form posts to mimic what the user would do in a browser. Simple request replays will not work.
You may also need to pass __EVENTARGUMENT hidden field. And don't forget to set name attribute, as well as id attribute.

How can I redirect to an URL with custom headers using C#?

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.

ASP.Net Creating Server Side Posting to external website with redirect

On a button click event I am required to POST to a page on an external website and redirect there. I get how to do this using a GET method
Reponse.Redirect("www.somesite.com?my=params&as=aget")
But how can I do this on as POST?
I don't want to post the entire form as this button event is called within a repeater
Depends.
If you want to post the exact input of a form you have on your site (that is, you just replicate a form the other site has), then just set the form's action to the URL you want to post to and the browser will do everything for you.
If however you want to post some values you generate on the server (perhaps based on the input from your form), I'm afraid it's not possible. You can't redirect using a POST. Redirect is GET by it's nature.
BUT you might be able to fake it by doing a POST (using something like System.Net.WebClient) and then a redirect (it depends on how the other site handles the GET - it might display the same thing that it did on the POST, or not).
One more option (for the second case) would be to to do an AJAX call to your server, which will compute the required values, then do the POST to the other server from Javascript.
You can build up the request using WebClient, adding the appropriate headers.
My inner forms don't contain the runat="server" attribute so I can do what I want. I do get this problem though ASP.Net First inner form in Server Form doesn’t POST.
Jquery is life saving in this situation. Used for one of my project and works like a charm. Give it a try : Peter Finch - Using Javascript to POST data between pages

append query string to URL on event (C#)

my client has a website that currently makes requests on a particular event (click, load etc). the website is working well and all but he has a problem with getting the website statistics with Google Analytics, the reason is, because the website never does a re-direct to a different page, it is all within the same page and no matter what event is loaded in the website(be it a video, tables etc) everything are displayed under the same url
www.somewebsite.com/default.aspx
what I want to achieve is on a particular event, change the url to
www.somewebsite.com/default.aspx?type=abc&id=999
How can I do this,. what is the easiest method to do this? please advise. The whole website is built in C#.
Many Thanks.
Is this event happening on the server or the client?
If it's on the server, you can call Response.Redirect and add your new query string parameter to the current url.
If it's on the client (Javascript), you can set the location property.
If you want to preserve your page's state, try adding your querystring parameter to the form's action parameter in Javascript.
Alternatively, as jeffamaphone suggested, you can change the hash (the part after the # sign) by setting location.hash without posting back to the server.
Actually, you should probably move some of the elements to different pages... this is based on what you said:
because basically all I am doing is
hiding and showing elements based on
events, if i do a response.redirect,
it will reload teh homepage.
If I understand correctly, the user is never redirected to a different page, you are just hiding/unhiding elements from default.aspx based in query strings... correct? the simplest solution will be to split that content into different aspx pages.

Categories