在AJAX时旋转光标

时间:2012-08-07 11:58:53

标签: c# jquery asp.net html ajax

我的很多网页都有一个列出产品的WebGrid。我有以下代码将项添加到用户单击的数据库:

        public bool ToCart(int userId,
            string partNumber,
            string productDescription,
            int units,
            int boxes,
            decimal unitPrice,
            decimal boxPrice,
            decimal lineTotal,
            string orderId,
            DateTime dateTime,
            bool isBoxed)
        {
            bool addedToCart = false;

            try
            {
                Cart cart = new Cart()
                {
                    UserId = userId,
                    PartNumber = partNumber,
                    Description = productDescription,
                    Units = units,
                    Boxes = boxes,
                    UnitPrice = unitPrice,
                    BoxPrice = boxPrice,
                    LineTotal = lineTotal,
                    OrderId = orderId,
                    OrderDate = dateTime,
                    IsBoxed = isBoxed
                };

                database.AddToCarts(cart);
                database.SaveChanges();

                addedToCart = true;
            }
            catch (Exception exception)
            {
                addedToCart = false;
                Console.Write(exception.Message);
            }

            return addedToCart;
        }

对此方法的调用如下所示:

ToCart(WebSecurity.CurrentUserId, PartNumber, ProductDescription, Units, Boxes, UnitPrice, BoxPrice, LineTotal, OrderId, DateTime.Now, IsBoxed)

现在我想把它变成一个AJAX帖子。但我不想要任何幻想。我只想将正常的WaitCursor或BusyCursor添加到购物车中,并在页面顶部显示<p>item added to cart</p>,当它已添加到购物车时。

enter image description here

如果用户点击要添加到购物车中的商品,我该如何做到这一点?

3 个答案:

答案 0 :(得分:1)

我建议您使用BlockUI插件:

$('.addToCart').click(function(){
 $.ajax({
       before: function(){$('body').block()} ,//will be called before the ajax call begins
       complete: function(){$('body').unblock()}, //will be called when ajax completes, whether with error or success
       //on success, append message to top
       success: function(){
              var message = "<p>item added to cart</p>";
              $(message).appendTo('.topDiv');
    }

    });
});

答案 1 :(得分:0)

创建一个div(在我下面的示例中,我给了我一个 loadingdiv id,其中包含您喜欢的任何内容(通常是动画GIF - 请查看{{3} })。然后,使用jQuery,您可以这样做:

<div id="loadingdiv"><img src="spinning-image.gif" /></div>

$("#loadingdiv").
    hide().
    ajaxStart(function() { $(this).show(); }).
    ajaxStop(function() { $(this).hide(); });

或者如果您只想更改光标,请执行以下操作:

$(document).
    ajaxStart(function() { $(document).css("cursor", "wait"); }).
    ajaxStop(function() { $(document).css("cursor", "default"); });

答案 2 :(得分:0)

在您的代码中添加:

using System.Web.Services;

并创建要使用AJAX调用的方法,将WebMethod属性添加到方法:

    [WebMethod]
    public static string CallAJAX(string Iwant)
    {
        if (string.IsNullOrEmpty(Iwant)) throw new Exception("What You want ?");
        return "One " + Iwant + " for You";
    }

这就是所有的C#部分。 现在从您的页面调用它将脚本管理器添加到页面表单:

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />

添加JavaScript方法:

<script type="text/javascript">

    function CallAJAX() {
        var Iwant = 'ice cream';
        PageMethods.CallAJAX(Iwant, OnSucceeded, OnFailed);
        //set wait cursor
        jQuery("body").css("cursor", "progress");
    }

    function OnSucceeded(result) {
        alert(result);
        //set cursor to normal
        jQuery("body").css("cursor", "auto");
    }

    function OnFailed(error) {
        alert(error.get_message());
        //set cursor to normal
        jQuery("body").css("cursor", "auto");
    }

</script>

使用PageMethods.CallAJAX(Iwant,OnSucceeded,OnFailed);您调用服务器C#方法并附加响应事件。 然后您可以将它与ASP.NET按钮一起使用,例如:

<asp:Button runat="server" Text="ajax call" OnClientClick="CallAJAX(); return false;" />
相关问题