Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: AJAX Coding in ASP.NET Web Forms

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Thu, Apr 19, 2012, 8:13 AM


JavaScript Code

var objJSON = {
    args: {
        ItemID: itemID,
        Quantity: qty
    }
};

var encoded = JSON.stringify(objJSON);

$.ajax({
    type: 'POST',
    url: 'PageName.aspx/MethodName',
    contentType: 'application/json; charset=utf-8',
    data: encoded,
    processData: false,
    datatype: 'json',
    async: false,
    success: function (data) {
        if (!data.d.Success) {
            alert(data.d.Message);
        }
        else {
            $(".ShoppingCartQty").html(data.d.CartItemCount);
            highlightFlash('.ShoppingCart');
        }
    }
});

ASPX Page

The ASPX page contains only the <@Page %> directive.

ASPX Code-Behind

Note these important elements in the following code.

  • The method is decorated with the
    [WebMethod]
    attribute.
  • The function signature is marked
    public static


[WebMethod]
public static OrderManagerResponse AddItems(AddItemsArgs args)
{
    var result = DoSomething(new Guid(args.ItemID), args.Quantity);
    return result;
}

public class AddItemsArgs
{
    public string ItemID { get; set; }
    public int Quantity { get; set; }
}

public class OrderManagerResponse
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public int CartItemCount { get; set; }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.