0

I have i jqgrid in MainPage.aspx, and i get data from a JSON page (page JsonSource.aspx). This JSON page get data from a DataTable. And i want to get this DataTable in page A (i don't want get data from database again).

THis is MainPage.aspx:

jQuery("#jQGridDemo").jqGrid({
    url: 'http:///JsonSource.aspx',
    datatype: "json",

In JsonSource.aspx:

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = InitDataSource();
        string json = ConvertDataTabletoString(dt);
        ////context.Response.Write(json);
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.Write(json);
        Response.End();
    }

If this is impossible, can you give me another solution. My data is huge, and i want get it only 1

Futureman
  • 309
  • 2
  • 5
  • 11
  • 2
    You could use session, cache or even cookies. I would probably use cache if the data is not bound to a specific user. – scheien Dec 18 '13 at 09:40
  • if your data is not changing between the two pages use cache and set an expiry. if it expires check database and load cache again. – Zaki Dec 18 '13 at 09:42
  • Look at [the answer](http://stackoverflow.com/a/8436273/315935) and [this one](http://stackoverflow.com/a/8547852/315935) or try [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage2_single.htm). The answers shows how one can use `localStorage` of web browser to save jqGrid state. Just try to resize width of some columns, hide some columns, set some filter (like type 1 in the filter for "Client" column and press Enter to apply the filter) and then *reload* **the page** (press F5). You will see that the demo saves the state between visits. – Oleg Dec 18 '13 at 10:02
  • in my environment, i can't use session :( – Futureman Dec 18 '13 at 10:26
  • @Oleg: Unfortunately he would need to query the database if there are other users (browsers). However in a single browser scenario, it's highly viable. – scheien Dec 18 '13 at 11:26
  • @scheien: I don't sure that the user's preferences will be saved in the database. **The data for the grid** will be set in the databse. I understand the question so: the user opened the grid, maked some columns hidden, changed the order of columns, changed the width of some columns, set some filters to display only the subset of data which important for him. Now he change to another page and then come back to continue his job, but all the grid's settings are away. In the case the solution with saving the user's preferences *on the specified page* in `localStorage` are very practical. – Oleg Dec 18 '13 at 11:47
  • @scheien: Even if the settings will be saved in the database one can use almost the same code like in the demo with `localStorage`. Instead of saving data in the `localStorage` one can make Ajax call to the server to save user's preferences. At the loading of the page (or the grid) one need get user's preferences previously saved in the database. So the main part of solution: how to get changes of jqGrid UI after the user do this. This shows the referenced demo. – Oleg Dec 18 '13 at 11:50

1 Answers1

0

You could use atleast 3 approaches here. Cache, sessions or even cookies (not that viable).

Cache would be the best option is the data is not bound to a specific user or a given session.

As @Zaki mentioned in a comment, you could invalidate the cache after x minutes, then reload if necessary.

Simple cache handling:

public static readonly ObjectCache cache = MemoryCache.Default; // this would be an instance variable

public void AddToCache<T>(string key, T item)
    {
        cache.Add(key, item, DateTime.Now.AddMinutes(CACHE_TIMEOUT));
    }

public T GetFromCache<T>(string key) where T : class
    {
        try
        {
            return (T)cache[key];
        }
        catch
        {
            return null;
        }
    }

Simple session handling:

 Session["MyData"] = json; // set data to session variable MyData

string jsonData = Session["MyData"]; // get the data.

You should however do some checks to make this a tad more robust.

scheien
  • 2,457
  • 1
  • 19
  • 22