3

I've spent the last night trying to figure this out.

Basically in Google Maps, I am able to generate directions, or waypoints, between two points that the user chooses in client side Javascript.

I ideally want to be able to store these in my database ( Im using C#.NET and SQL Server DB ) by passing these to a server side C# method..

I've got to the point where I can put the directions I need into a string by using:

*var string = JSON.stringify(response);*

Now, here's where I'm stuck.

How do I pass this to a C# webforms method?

I've seen an MVC C# solution to my problem as:

var str = JSON.stringify(data)

var city = {};
        city.Directions = str;
        $.ajax({
            type: 'POST',
            url: 'usertrip.aspx/GetDirections',
            data: str ,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.Directions);;
                }
        });

But I've tested, and have concluded that this won't work for webforms. Does anyone know how I can alter this code so that I can pass the string to a Webforms method and not MVC?

Thanks!

Loktar
  • 34,764
  • 7
  • 90
  • 104
Donald Cardwell
  • 71
  • 1
  • 3
  • 10

2 Answers2

4

You can definitely do this kind of thing with webforms. The important thing is that you need to set up a webservice that exposes methods that can be hit by the ajax call. This awesome article called Using jQuery to directly call ASP.NET AJAX page methods proved invaluable for me to figure out how to accomplish what you're trying to do.

For an example (from the article) doing something like this:

public partial class _Default : Page 
{
  [WebMethod]
  public static string DoSomething(string myJsonData)
  {
    // deserialize your JSON
    // do something cool with it
  }
}

Would allow you to hit the webmethod with your AJAX call. I can assure you that I've done this in many different asp.net solutions what don't use MVC so with a little bit of tinkering you should be able to get the information you need to your code behind.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • Hi Jesse, I have tried the code from the same article, everything works except the c# code cannot pick up webform data, the NameValue[] formVars is empty. Do you have any suggestion on this. My post is here . http://stackoverflow.com/questions/20311890/error-with-using-jquery-to-post-form-data-to-an-asp-net-asmx-ajax-web-service – June Dec 07 '13 at 12:17
2

You would need to do something like :

var str = JSON.stringify(data)

var city = {};
        city.Directions = str;
        $.ajax({
            type: 'POST',
            url: 'usertrip.aspx/GetDirections',
            data: { city: str },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.Directions);;
                }
        });

And in the Webforms code behind:

 City city = new JavaScriptSerializer().Deserialize<City>(Request.Form["city"]);
Steve
  • 2,988
  • 2
  • 30
  • 47