0

I have a page where a user submits multiple keywords before finding resources based on those keywords. I have a jQuery array that loads all the keywords and I want to pass that jQuery array to the next results page. I'm trying to figure out the best method to do this. I've tried putting the array into the string and passing it through the URL, but was unable to properly URLEncode it. I've also tried using window.location but this has been not working well either. This seems simple but I'm going in circles. It is a one dimensional array that I want to hold in memory then pass it to the next aspx page. What's the best method? Below is my code as it stands now which is not correct.

//When get resources button clicked show current array values
$("#nihSearch_btnResults").click(function () {
    //get number of tabs in the holder
    gettabs = $("div#keys > div.tab").size();
    output = "";
    //loop through the tabs        
    $("div.tab span").each(function () {
        //get the text of the tab
        var kw = $(this).text();
        //add it to the array
        keywords.push(kw);
    });

    var jsonstring = JSON.stringify(keywords);
    alert(jsonstring);
});
Jason
  • 59
  • 1
  • 2
  • 10

2 Answers2

0

I was able to do this through the URL. I converted the array to a string, replaced all spaces with underscores then used window.location to move to the next page. Once there, I use the Page_Load in the C# page to replace the underscores back to spaces and use "split" to re-create the array.

jQuery

//convert array into a string
    var keystring = keywords.join(",");
    //make keystring all lower case and replace spaces with hyphens
    keystring = keystring.toLowerCase().replace(/ /g, '_');
    //Send it to the next page.
    window.location = "Results.aspx?kws=" + keystring;

C#

protected void Page_Load(object sender, EventArgs e)
    {
        //get keywords from URL
        string urlkeys = Request.QueryString["kws"];
        //replace dashes with spaces
        urlkeys = urlkeys.Replace("_", " ");
        //split back into an array
        string[] arr = urlkeys.Split(',');            
    }
Jason
  • 59
  • 1
  • 2
  • 10
0

You can post the client side data to server using $.ajax method and asp.net server side web-method

Here's the code which I had implemented
http://blog.nitinsawant.com/2011/09/draft-sending-client-side-variables-to.html

         //js code to post data to server
         var jsonData = "{'jsonData':'" + JSON.stringify(keywords) + "'}";//create string representation of the js object
         $.ajax({
            type: "POST",
            url: 'Test.aspx/AcceptData',
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: ($.browser.msie) ? "text" : "json",
            success: function(msg) {
                //call successfull
                var obj = msg.parseJSON();
                alert(obj.d); //d is data returned from web services

                //The result is wrapped inside .d object as its prevents direct execution of string as a script
            },
            error: function(xhr, status, error) {
                //error occurred
                alert(xhr.responseText);
            }
        });

web method:

//C# code
[System.Web.Services.WebMethod]
 public static string AcceptData(object jsonData)
 {
     Customer newCust =(Customer)JsonConvert.DeserializeObject(jsonData.ToString(),typeof(YourCustomClass));
     return "Server response: Hello "+newCust.FirstName;
 }

Regards
Nitin

Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98