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

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

  概述

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

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

  先介紹一個(gè)Jquery插件:Tab,可以到http://jqueryui.com/download上下載。看下它自帶一個(gè)例子的截圖:

ddd

  效果圖:

gggg

  實(shí)現(xiàn)

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

    <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多來(lái)了一個(gè)getContentTab函數(shù)。他有兩個(gè)參數(shù),用于表示你要顯示

  哪個(gè)tab頁(yè)的第幾頁(yè)數(shù)據(jù)。這里默認(rèn)加載的時(shí)候,顯示tabs-Shoes的第一頁(yè)數(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之前要先寫好控制器的代碼,很簡(jiǎn)單,基本上沒(méi)有代碼:

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

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

<%@ 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>

  我們?cè)偻ㄟ^(guò)一個(gè)ajax調(diào)用來(lái)將這個(gè)控件顯示在ViewCategory對(duì)應(yīng)的View上,定義一個(gè)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)フ?qǐng)求服務(wù)端的ViewByCategory方法,獲取table中的數(shù)據(jù)。看ViewByCategory的代碼:

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);
}

  為了簡(jiǎn)單起見數(shù)據(jù)來(lái)來(lái)自內(nèi)存,使用list的take來(lái)實(shí)現(xiàn)分頁(yè)。你可以很方便的改成從DB獲取數(shù)據(jù)。在看下如何生成分頁(yè)的html,其實(shí)很簡(jiǎn)單,我們只要在生成的分頁(yè)的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頁(yè)中的異步無(wú)刷新分頁(yè)。這東西太常用了,放在這里,希望對(duì)你有所幫助。

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

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

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

主站蜘蛛池模板: 深爱综合网 | 91精品成人 | 亚洲男人的天堂久久香蕉 | 日本一区二区视频 | 亚洲永久免费 | 免费视频亚洲 | 成人午夜在线播放 | 91嫩草国产线免费观看 | 欧美在线性视频 | 欧美人与zoxxxx视频 | 欧美激情 在线 | 美女一级ba大片免色无遮住 | 黄视频观看 | 一二三四视频社区5在线高清视频 | 福利视频91| 怡红院视频在线观看最新 | 国产福利在线播放 | 东京加勒比中文字幕波多野结衣 | 国产成人午夜精品5599 | 91高清视频在线 | 国色天香精品亚洲精品 | 91久久国产 | 一区二区三区在线播放视频 | 成人久久久久久 | 国产精品高清一区二区三区 | 交资源网在线观看 | 九九久久国产 | 韩国一级永久免费观看网址 | 免费一区二区三区免费视频 | 精品免费一区二区三区 | 亚洲妇人成熟性成熟美女 | 国产中文字幕在线免费观看 | 欧美xxxxxxxx| 第一页在线视频 | 成人福利在线 | 激情综合婷婷 | 影音先锋 色天使 | 国语自产自拍秒拍在线视频 | 深爱五月激情网 | 露脸国产自产拍在线观看 | 成人毛片在线视频 |