user549640
user549640

Reputation: 11

asp.net state save page

I have a web page in asp.net which contains a textbox , a button and a grid view.gridview loads some content from database and textbox and button is used to apply search. Now my problem is that when i search some and the search results are loaded in grid view. If i click the edit button in grid view then i redirect to new page and after finishing editing and click back on edit page. when i get back to my page the textbox is blank and all. I want my state to be saved when i come back to page

Upvotes: 1

Views: 1490

Answers (2)

Dalorzo
Dalorzo

Reputation: 20014

The issue as your post correctly states this is an issue about "State". ASP.NET offers different options to keep the information between round trips.

On Client Side:

You can store information in the Page by using:

ViewState, Hiddend Fields: Viewstate is actually a hidden field and data will be lost after leaving the page.

Or at site level:

Cookies: These can be temporary or permanent and has limitations in the amount of memory they can store.

And you if want communication between two pages it is also possible to use the

Query String: It is what is appended at the of the end url followed by ?. Example: ?category=basic&price=100. However this also has length limitations. In IE for instance is 2083 characters.

On Server Side

You can use:

Session or Application State: Both are Key/Value objects the difference is that Session will end when the users leaves and its values are unique per user while the application is available across all sessions and will remain available as long the application is up.

With your problem at hand: You can use 2 of these:

  1. The Session: to store the search value in the server side, or
  2. Use the query string to communicate across pages

In my opinion the simpler to use is the Session even though it may have some draw back if you have multiple instances of the same page opened. However since the purpose is to store a search value the main advantage(recommended) has it querystring because this will not only allow to you to save a search and will not have the issue the session has of multiple instances you may be able to share a search by sharing the "URL" like several search engine does.

You can find here how to add values to the query string:

Querystring - Add values to querystring in c#

and to read those values it is only necessary to use: Request.QueryString("var name")

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 156948

I would 'save' this info in the redirect url, which you will open after Save.

  1. On search, pass the search parameter as url parameter.
  2. On the 'edit' button, add '?returnUrl=... after the url .
  3. In the edit page, on clicking Save you probably have some code to save. If successful, call Response.Redirect with the url you find in the Request.Parameters.

Upvotes: 2

Related Questions