MvcPager samples - Ajax events
This sample demonstrate how to handle Ajax events when implement Ajax paging, click or select page 10 will trigger server exception and fire the client OnFailure Ajax event.
Please note: AjaxOptions.OnSuccess property is not implemented when using jQuery Ajax
Please note: AjaxOptions.OnSuccess property is not implemented when using jQuery Ajax
| Order ID | Order Date | Customer ID | Ship Address |
|---|---|---|---|
| 10920 | 3/3/1998 12:00:00 AM | AROUT | Brook Farm Stratford St. Mary |
| 10921 | 3/3/1998 12:00:00 AM | VAFFE | Smagsloget 45 |
| 10922 | 3/3/1998 12:00:00 AM | HANAR | Rua do Paço, 67 |
| 10923 | 3/3/1998 12:00:00 AM | LAMAI | 1 rue Alsace-Lorraine |
| 10924 | 3/4/1998 12:00:00 AM | BERGS | Berguvsvägen 8 |
| 10925 | 3/4/1998 12:00:00 AM | HANAR | Rua do Paço, 67 |
| 10926 | 3/4/1998 12:00:00 AM | ANATR | Avda. de la Constitución 2222 |
| 10927 | 3/5/1998 12:00:00 AM | LACOR | 67, avenue de l'Europe |
| 10928 | 3/5/1998 12:00:00 AM | GALED | Rambla de Cataluña, 23 |
| 10929 | 3/5/1998 12:00:00 AM | FRANK | Berliner Platz 43 |
| 10930 | 3/6/1998 12:00:00 AM | SUPRD | Boulevard Tirou, 255 |
| 10931 | 3/6/1998 12:00:00 AM | RICSU | Starenweg 5 |
| 10932 | 3/6/1998 12:00:00 AM | BONAP | 12, rue des Bouchers |
| 10933 | 3/6/1998 12:00:00 AM | ISLAT | Garden House Crowther Way |
| 10934 | 3/9/1998 12:00:00 AM | LEHMS | Magazinweg 7 |
| 10935 | 3/9/1998 12:00:00 AM | WELLI | Rua do Mercado, 12 |
| 10936 | 3/9/1998 12:00:00 AM | GREAL | 2732 Baker Blvd. |
| 10937 | 3/10/1998 12:00:00 AM | CACTU | Cerrito 333 |
| 10938 | 3/10/1998 12:00:00 AM | QUICK | Taucherstraße 10 |
| 10939 | 3/10/1998 12:00:00 AM | MAGAA | Via Ludovico il Moro 22 |
jQuery Ajax:
MicrosoftAjax:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PagedList<Order>>" %>
<%@ Import Namespace="Webdiyer.Mvc2Demo.Models"%>
<%@ Import Namespace="Webdiyer.WebControls.Mvc"%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MvcPager samples - Ajax events
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script language="javascript" type="text/javascript"
src="<%=Url.Content("~/Scripts/jquery-1.4.2.min.js") %>"></script>
<script language="javascript" type="text/javascript"
src="<%=Url.Content("~/Scripts/MicrosoftAjax.js") %>"></script>
<script language="javascript" type="text/javascript"
src="<%=Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>"></script>
<h2>MvcPager samples - Ajax events</h2>
<div>This sample demonstrate how to handle Ajax events when implement Ajax paging,
click or select page 10 will trigger server exception and fire the client OnFailure Ajax event.
<br /><span style="color:red">Please note:
AjaxOptions.OnSuccess property is not implemented when using jQuery Ajax</span>
</div><br />
<%Html.RenderPartial("UCAjaxEvents", Model); %>
</asp:Content>
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<PagedList<Webdiyer.Mvc2Demo.Models.Order>>" %>
<%@ Import Namespace="Webdiyer.Mvc2Demo.Models" %>
<%@ Import Namespace="Webdiyer.WebControls.Mvc"%>
<div id="dvOrders">
<table width="98%">
<tr><th>Order ID</th><th>Order Date</th><th>Customer ID</th><th>Ship Address</th></tr>
<%foreach(Order od in Model)
{
%>
<tr><td><%=od.OrderID %></td><td><%=od.OrderDate.ToString() %></td>
<td><%=od.CustomerID %></td><td><%=od.ShipAddress %></td></tr>
<%
} %>
</table>
<p>jQuery Ajax:</p>
<%=Html.AjaxPager(Model, new PagerOptions() { PageIndexParameterName = "id",
ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList,
ShowGoButton = false }, new AjaxOptions() { UpdateTargetId = "dvOrders",
OnBegin = "function(){alert('Ajax OnBegin event fired');}",
OnComplete = "function(){alert('Ajax OnComplete event fired');}",
OnFailure = "function(){alert('Ajax OnFailure event fired');}" })%>
<p>MicrosoftAjax:</p>
<%=Ajax.Pager(Model, new PagerOptions() { PageIndexParameterName = "id",
ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList,
ShowGoButton = false }, new AjaxOptions() { UpdateTargetId = "dvOrders",
OnBegin = "function(){alert('Ajax OnBegin event fired');}",
OnComplete = "function(){alert('Ajax OnComplete event fired');}",
OnSuccess = "function(){alert('Ajax OnSuccess event fired');}",
OnFailure = "function(){alert('Ajax OnFailure event fired');}" })%>
</div>
public ActionResult AjaxEvents(int? id)
{
using (var db = new MvcPagerDemoDataContext())
{
if (id.HasValue && id.Value == 10) //throw exception for testing purpose
throw new Exception("test exception");
PagedList<Order> orders = db.Orders.ToPagedList(id ?? 1, 20);
if (Request.IsAjaxRequest())
return PartialView("UCAjaxEvents", orders);
return View(orders);
}
}