简体   繁体   中英

Need help - MasterPage always return null when call from WebMethod (ASP.NET) C#

I am building a shopping cart, and everything works fine. Now, I am trying to use jQuery to insert and show data from the database. I use jQuery to insert data successfully, but I cannot update the panel which is inside MasterPage, because the master always returns null.

Does anyone have any idea to solve my issue?

<script>
    $(document).ready(function () {
        $("[id*=lb1KgButton]").click(function () {


            $.ajax({
                data: JSON.stringify({

                    customerID: _customerID
                }),
                dataType: "json",
                type: "POST",
                url: '<%= ResolveUrl("~/AddToCart.aspx/AddToCartFrom1To4Kg") %>',
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    showSuccess('', 'Added to cart successfully!');
                }
            });
        });
    });
</script>

Here are code inside AddToCart.aspx

[WebMethod]
    public static string AddToCartFrom1To4Kg(string customerID)
    {
        string result = "";
        AddToCart atc = new AddToCart();

        atc.UpdateShoppingCart(); }

public void UpdateShoppingCart()
    {
        var master = Master as Home;
        if (master != null)
        {
            UpdatePanel upMasterPage = (UpdatePanel)master.FindControl("UpdatePanel1");
            master.CheckAddToCart();
            upMasterPage.Update();
        }
    }

You cannot update a page from a static method. You must return to the javascript method and update the page from there. Return data/info to the ajax call as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM