一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

ASP.NET MVC2.0在Tab頁中實(shí)現(xiàn)異步無刷新分頁

  概述

  很多地方都存在以Tab頁來呈現(xiàn)數(shù)據(jù)的方式,比如網(wǎng)易、新浪、搜狐、QQ等知名的門戶網(wǎng)站的首頁,還有大家熟知的博客園首頁,都是用了tab頁來顯示數(shù)據(jù)。大家之所以喜歡用Tab,因?yàn)樗艽蟠蟮脑黾语@示數(shù)據(jù)的空間,能在固定的空間中顯示更多的數(shù)據(jù)。分頁也是為了方便數(shù)據(jù)的顯示,在應(yīng)用系統(tǒng)中必不可少。這篇文章使用Jquery在ASP.NET MVC中使用Tab頁,以及在Tab頁中實(shí)現(xiàn)異步無刷新的分頁功能。估計(jì)這個大家都會用得到的。

  在ASP.NET MVC中實(shí)現(xiàn)分頁,在之前的一篇博文:ASP.NET MVC2右鍵菜單和簡單分頁中已經(jīng)實(shí)現(xiàn)了。實(shí)現(xiàn)的方式很簡單,在table下面加上一段<a/><a/><a/>...的html就行了。

  先介紹一個Jquery插件:Tab,可以到http://jqueryui.com/download上下載??聪滤詭б粋€例子的截圖:

ddd

  效果圖:

gggg

  實(shí)現(xiàn)

  按照它的Demo,在ASP.NET mvc項(xiàng)目中引入js和css,以及Jquery。我在ASP.NET MVC的母板頁中引入這些文件:

    <link href="http://www.cnblogs.com/Content/base/ui.all.css" rel="stylesheet" type="text/css" />
<
script src="http://www.cnblogs.com/Scripts/jquery-1.4.1.js" type="text/Javascript"></script>
<
script src="http://www.cnblogs.com/Scripts/ui.core.js" type="text/Javascript"></script>
<
script src="http://www.cnblogs.com/Scripts/ui.tabs.js" type="text/Javascript"></script>

  引入之后,參考它的Demo在MVC項(xiàng)目中View中使用Tab。 可以看到比tab自帶的demo多來了一個getContentTab函數(shù)。他有兩個參數(shù),用于表示你要顯示

  哪個tab頁的第幾頁數(shù)據(jù)。這里默認(rèn)加載的時候,顯示tabs-Shoes的第一頁數(shù)據(jù)。

   <script type="text/Javascript">
$(document).ready(function () {
$("#tabs").tabs();
getContentTab('Shoes', 1);
});
</script>
<
div id="tabs">
<
ul>
<
li><a href="#tabs-Shoes" onclick="getContentTab('Shoes',1);">Shoes</a></li>
<
li><a href="#tabs-Electronics" onclick="getContentTab('Electronics',1);">Electronics</a></li>
<
li><a href="#tabs-Food" onclick="getContentTab('Food',1);">Food</a></li>
</
ul>
<
div id="tabs-Shoes">
</
div>
<
div id="tabs-Electronics">
</
div>
<
div id="tabs-Food">
</
div>
</
div>

  當(dāng)然在定義View之前要先寫好控制器的代碼,很簡單,基本上沒有代碼:

public ActionResult ViewCategory()
{
return View();
}

  好了,下面開始我們重要的幾步。顯示table以及實(shí)現(xiàn)table的分頁。這個demo的tab定義了三個tab頁,每一頁的table結(jié)構(gòu)是一樣的,所以我定義一個用戶控件來實(shí)現(xiàn)table和分頁。代碼如下:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcAjaxPaging.Models.ProductViewModel>" %>
<%
@ Import Namespace="MvcAjaxPaging.HtmlHelpers"%>
<table class="grid">
<
thead>
<
tr>
<
th>Product name</th>
<
th>Category</th>
</
tr>
</
thead>
<
tbody>
<% foreach (var product in ViewData.Model.Products) { %>
<tr>
<
td><%= product.Name %></td>
<
td><%= product.Category %></td>
</
tr>
<% } %>
</tbody>
</
table>
<
div class="pager">
<%= Html.Pager(ViewData.Model.PagingInfo.CurrentPage, ViewData.Model.PagingInfo.ItemsPerPage, ViewData.Model.PagingInfo.TotalItems, "", ViewData["CategoryDisplayName"] as string)%>
</div>

  我們再通過一個ajax調(diào)用來將這個控件顯示在ViewCategory對應(yīng)的View上,定義一個js函數(shù):

function getContentTab(categoryName, page) {

var url = '<%= Url.Content("~/MyPaging/ViewByCategory/") %>' + categoryName + "/" + page;
var targetDiv = "#tabs-" + categoryName;
$.get(url, null, function (result) {
$(targetDiv).html(result);
});
}

  我們看上面代碼,我們?nèi)フ埱蠓?wù)端的ViewByCategory方法,獲取table中的數(shù)據(jù)??碫iewByCategory的代碼:

public ActionResult ViewByCategory(string categoryName, int? page)
{
categoryName = categoryName ?? this.categories[0];
int currentPageIndex = page.HasValue ? page.Value : 0;
ProductViewModel productViewModel = new ProductViewModel();

IList<Product> productsByCategory = this.allProducts.Where(p => p.Category.Equals(categoryName)).ToList();
productViewModel.Products = productsByCategory.Skip((currentPageIndex - 1) * 10).Take(10).ToList();
productViewModel.PagingInfo.CurrentPage = currentPageIndex;
productViewModel.PagingInfo.ItemsPerPage = 10;
productViewModel.PagingInfo.TotalItems = productsByCategory.Count;
return View("ViewByCategoryTable", productViewModel);
}

  為了簡單起見數(shù)據(jù)來來自內(nèi)存,使用list的take來實(shí)現(xiàn)分頁。你可以很方便的改成從DB獲取數(shù)據(jù)。在看下如何生成分頁的html,其實(shí)很簡單,我們只要在生成的分頁的HTML中使用getContentTab函數(shù)就行了。

public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords,string urlPrefix,string status)
{
StringBuilder sb1 = new StringBuilder();

int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);

if (currentPage > 0)
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >Previous</a>", status, currentPage));

if (currentPage - currentPageSize >= 0)
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >...</a>", status, (currentPage - currentPageSize) + 1));

for (int i = seed; i < Math.Round((totalRecords / currentPageSize) + 0.5) && i < seed + currentPageSize; i++)
{
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >{1}</a>", status, i + 1));
}

if (currentPage + currentPageSize <= (Math.Round((totalRecords / currentPageSize) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >...</a>", status, (currentPage + currentPageSize) + 1));

if (currentPage < (Math.Round((totalRecords / currentPageSize) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >Next</a>", status, currentPage + 2));

return sb1.ToString();
}

  效果

jjjj

kkkk

  總結(jié):ASP.NET mvc中實(shí)現(xiàn)了在tab頁中的異步無刷新分頁。這東西太常用了,放在這里,希望對你有所幫助。

  代碼:http://cid-aef1e64945224a20.office.live.com/self.ASPx/.Public/MvcAjaxPaging.rar

NET技術(shù)ASP.NET MVC2.0在Tab頁中實(shí)現(xiàn)異步無刷新分頁,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 99视频精品全部国产盗摄视频 | 国内久久精品视频 | 国内精品自在自线2020 | 成人激情视频网站 | 丁香婷婷网 | 一区一精品 | 日韩亚洲欧美一区噜噜噜 | 一本色道久久88综合亚洲精品高清 | 欧洲视频在线观看 | 国产精品麻豆一区二区三区v视界 | 欧美午夜久久 | 国产伦精一区二区三区 | 国产网红在线观看 | 黄色美女网站免费 | 狠狠色网 | 成人国产一区 | 狠狠做久久深爱婷婷97动漫 | 欧美经典成人在观看线视频 | 91av福利| 青青国产成人久久91网 | 国产在线视频91 | 欧美a免费 | 婷婷在线视频观看 | 国产亚洲一区呦系列 | 男人女人黄 色视频免费 | 中文字幕二区三区 | 免费播放一区二区三区 | 手机在线日韩高清理论片 | 欧美图片另类小说综合 | 手机在线亚洲 | 91麻豆tv | 国产欧美激情一区二区三区-老狼 | 伊人影院在线观看视频 | 日本乱中文字幕系列在线观看 | 亚洲国产成人最新精品资源 | 国产午夜亚洲精品不卡福利 | 视频一区在线观看 | 天天综合网在线 | 国产精品福利在线 | 亚洲第一综合网站 | 福利写真视频在线观看网站 |