How do I go about calling a method on an ASP.NET Web Form page using the getJSON method on jQuery?
The goal is this:
I don't want to use an UpdatePanel, I've done this hundreds on times using the ASP.NET MVC Framework, but can't figure it out using Web Forms!
So far, I can do everything, including calling the server, it just doesn't call the right method.
Thanks,
Kieron
Some code:
jQuery(document).ready(function() {
jQuery("#<%= AreaListBox.ClientID %>").click(function() {
updateRegions(jQuery(this).val());
});
});
function updateRegions(areaId) {
jQuery.getJSON('/Locations.aspx/GetRegions',
{ areaId: areaId },
function (data, textStatus) {
debugger;
});
}
Here is a minimalistic example which should hopefully get you started:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<script runat="server">
[WebMethod]
public static string GetRegions(int areaId)
{
return "Foo " + areaId;
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery and page methods</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
var areaId = 42;
$.ajax({
type: "POST",
url: "Default.aspx/GetRegions",
data: "{areaId:" + areaId + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data.d);
}
});
});
</script>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With