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 |
|---|---|---|---|
| 11060 | 4/30/1998 12:00:00 AM | FRANS | Via Monte Bianco 34 |
| 11061 | 4/30/1998 12:00:00 AM | GREAL | 2732 Baker Blvd. |
| 11062 | 4/30/1998 12:00:00 AM | REGGC | Strada Provinciale 124 |
| 11063 | 4/30/1998 12:00:00 AM | HUNGO | 8 Johnstown Road |
| 11064 | 5/1/1998 12:00:00 AM | SAVEA | 187 Suffolk Ln. |
| 11065 | 5/1/1998 12:00:00 AM | LILAS | Carrera 52 con Ave. Bolívar #65-98 Llano Largo |
| 11066 | 5/1/1998 12:00:00 AM | WHITC | 1029 - 12th Ave. S. |
| 11067 | 5/4/1998 12:00:00 AM | DRACD | Walserweg 21 |
| 11068 | 5/4/1998 12:00:00 AM | QUEEN | Alameda dos Canàrios, 891 |
| 11069 | 5/4/1998 12:00:00 AM | TORTU | Avda. Azteca 123 |
| 11070 | 5/5/1998 12:00:00 AM | LEHMS | Magazinweg 7 |
| 11071 | 5/5/1998 12:00:00 AM | LILAS | Carrera 52 con Ave. Bolívar #65-98 Llano Largo |
| 11072 | 5/5/1998 12:00:00 AM | ERNSH | Kirchgasse 6 |
| 11073 | 5/5/1998 12:00:00 AM | PERIC | Calle Dr. Jorge Cash 321 |
| 11074 | 5/6/1998 12:00:00 AM | SIMOB | Vinbæltet 34 |
| 11075 | 5/6/1998 12:00:00 AM | RICSU | Starenweg 5 |
| 11076 | 5/6/1998 12:00:00 AM | BONAP | 12, rue des Bouchers |
| 11077 | 5/6/1998 12:00:00 AM | RATTC | 2817 Milton Dr. |
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);
}
}