1

I am working on asp.net web forms application.I need to access datatable returning from database in javascript/jquery.

But problem is that I am not able to get value. I tried declaring it on top of class as well as as session but didn't work.

I am getting blank if I try to alert it.

Here is my code..

On page load.. there are nested methods which are being used to load data inside GridView. Now I want to get same data in client side as well so that I can use to show in on Google map..

On Page_load event my below is code to get data from database

this.gvGmap.DataSource = null;
        GmapDataTable = GetDataTable("someparameter to get data from db");
        Session["GmapDataTable"] = GmapDataTable;
        this.gvGmap.DataSource = GmapDataTable;
        this.gvGmap.DataBind();

Now I tried two different approach to get this data client side.. but it's blank

1st

var mJSVariable = <%:GmapDataTable %>;
    alert(mJSVariable);

2nd session approach

var yourVariable = '<%= Session["GmapDataTable"] %>';
    alert(yourVariable);
Mahajan344
  • 2,492
  • 6
  • 39
  • 90
  • Have you tried `var mJSVariable = '<%:GmapDataTable %>';`? – Jerdine Sabio Oct 23 '18 at 06:10
  • yes.. when rendered it's showing blank...may be javascript is executed first but server side code.. still running – Mahajan344 Oct 23 '18 at 06:14
  • Is that `GmapDataTable` contains `DataTable` object? What you're mean with "blank" - is it contain no value or the type name of `DataTable`? – Tetsuya Yamamoto Oct 23 '18 at 06:15
  • from server side.. it will return datatable.. but when rendered.. it's showing var sessionValue = ''; var yourVariable = ''; – Mahajan344 Oct 23 '18 at 06:20
  • Probably you need to serialize it, either using `JavaScriptSerializer` or `JsonConvert.SerializeObject` to pass `DataTable` contents inside ASPX page as JSON and decode it on JS variable assignment with `JSON.parse()`. – Tetsuya Yamamoto Oct 23 '18 at 06:24

2 Answers2

0

If you data is just linked with your current page and not huge then use viewstate instead of session this will not create much load on your server. Instead of accessing session directly in client side assign it to a property this will make your code more reuseable.You can searilize your data table.View State Vs session

Although using session you can do with the following way.

   `public static DataTable GmapDataTableProperty
    {
        set { HttpContext.Current.Session["GmapDataTable"] = value; }
        get
        {
            if (HttpContext.Current.Session["GmapDataTable"] != null)
            {
                return (DataTable)HttpContext.Current.Session["GmapDataTable"];
            }

            return null;
        }
    }

GmapDataTableProperty = GmapDataTable; `

Access it on client side like

var mJSVariable = <%= GmapDataTableProperty %

Hamza Haider
  • 730
  • 6
  • 20
  • Imagine the `DataTable` size is large (more than thousand rows), the `ViewState` may incur performance penalty on `Page_Load` event. I strongly against usage of it to store potentially large objects. – Tetsuya Yamamoto Oct 23 '18 at 06:30
  • yes you are right if the data is huge then go with session – Hamza Haider Oct 23 '18 at 06:35
0

your approach via session is right, but you need to convert your session object into DataTable object, make sure your session variable is not null.

var myValue = '<%= ((System.Data.DataTable)Session["dt"]).Rows[0][0] %>';
alert(myValue);