【问题标题】:Spinning Cursor while AJAXAJAX 时旋转光标
【发布时间】:2012-08-04 10:31:45
【问题描述】:

我在很多列出产品的页面中都有一个 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>

当用户点击他们希望添加到购物车的商品时,我该如何实现?

【问题讨论】:

标签: c# jquery asp.net html ajax


【解决方案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');
    }

    });
});

【讨论】:

  • 谢谢@ftom2 - 我没有想过要阻止 UI(我应该有,因为我来自 Windows 窗体并且已经做了很多) - 我要给这个现在旋转
  • 请问,当用户点击链接(他们想要添加到购物车的项目)时,我将如何“执行”这个 ajax 请求?我是否应该将整个 ajax 代码包装在一个命名函数中,并从 &lt;a onclick="functionToCall()"&gt;some link&lt;/a&gt; 中调用该函数?
【解决方案2】:

创建一个 div(在下面的示例中,我给了我一个 loadingdivid),其中包含您喜欢的任何内容(通常是动画 GIF - 看看 http://ajaxload.info)。然后,使用 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"); });

【讨论】:

    【解决方案3】:

    在您的代码中添加:

    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>
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 2016-12-31
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多