2

I currently have a list of dates in my code-behind, I'd like to pass the list to a variable in javascript without the use of hiddenfield

For instance,

Aspx.cs:

List < DateTime > blockedDate = new List < DateTime > ();

foreach(DataRow blockedRow in phBLL.getAllPH(DateTime.Now.Year).Tables[0].Rows) 
{
   blockedDate.Add(Convert.ToDateTime(blockedRow["date1"]));
}

Aspx:

 $(document).ready(function () {

     var arrayOfDates = ""    
});

What I've tried

Aspx.cs:

public static List < DateTime > blockedDate = new List < DateTime > ();

[WebMethod]
public static List < DateTime > blockDates() 
{
  foreach(DataRow blockedRow in phBLL.getAllPH(DateTime.Now.Year).Tables[0].Rows) {
  blockedDate.Add(Convert.ToDateTime(blockedRow["date1"]));
 }

 return blockedDate;
}

Javascript:

    $.ajax({
     type: "POST",
     url: "CreateClass.aspx/blockDates",
     data: null,
     contentType: 'application/json; charset=utf-8',
     dataType: 'json',
     error: function(XMLHttpRequest, textStatus, errorThrown) {
         alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
     },
     success: function(result) {
         for (var i = 0; i < result.d.length; i++) {
             var dates = [new Date(parseInt(result.d[i].substr(6)))];
             console.log(dates);
         }
     }
 });

I am trying to get the result and place into an Array. So it'd end up something like this

an array of dates

var array = ["2016/11/14", "2016/11/15", "2016/11/16"];
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Arane
  • 323
  • 4
  • 19
  • Create a webmethod which contain your code behind logic and call it using ajax. – Divyang Desai Nov 20 '16 at 03:06
  • or print serialized json into page as value of the variable. Both approaches are very easy to research – charlietfl Nov 20 '16 at 04:01
  • @Div I tried, but I kept getting something like `[Object] object` or `\Date(random numbers)/` – Arane Nov 20 '16 at 05:08
  • @Arane: Add that code in the question. – Divyang Desai Nov 20 '16 at 05:25
  • @Div Posted! really do need help on this, thanks.. – Arane Nov 20 '16 at 05:30
  • @Arane: Yeah, you get something like `\Date(random numbers)/` because you cannot parse json to date directly, read [this](http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx) and also refer [this](http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript) – Divyang Desai Nov 20 '16 at 05:44
  • @Div Do you know how to put the result into an array like for instance like this `var array = ["2016/11/14", "2016/11/15", "2016/11/16"];`?? – Arane Nov 20 '16 at 05:47
  • @Arane: Do you know about [`JavaScript Array push() Method`](http://www.w3schools.com/jsref/jsref_push.asp) ? – Divyang Desai Nov 20 '16 at 05:52
  • 1
    @Div Managed to do it, at long last haha... thank you so much for the guidance and help! Much appreciated!! – Arane Nov 20 '16 at 06:07
  • Glad to know that :) – Divyang Desai Nov 20 '16 at 06:10

1 Answers1

1

As per comments, Create a [WebMethod] which contain your code behind logic and call it using ajax.

Now, you will get data on ajax success, and just push your data to array arrayOfDates using JavaScript Array push() Method

Hope this helps!

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76