中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

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

  概述

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

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

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

ddd

  效果圖:

gggg

  實現(xiàn)

  按照它的Demo,在ASP.NET mvc項目中引入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項目中View中使用Tab。 可以看到比tab自帶的demo多來了一個getContentTab函數(shù)。他有兩個參數(shù),用于表示你要顯示

  哪個tab頁的第幾頁數(shù)據(jù)。這里默認加載的時候,顯示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>

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

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

  好了,下面開始我們重要的幾步。顯示table以及實現(xiàn)table的分頁。這個demo的tab定義了三個tab頁,每一頁的table結構是一樣的,所以我定義一個用戶控件來實現(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對應的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)フ埱蠓斩说腣iewByCategory方法,獲取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);
}

  為了簡單起見數(shù)據(jù)來來自內(nèi)存,使用list的take來實現(xiàn)分頁。你可以很方便的改成從DB獲取數(shù)據(jù)。在看下如何生成分頁的html,其實很簡單,我們只要在生成的分頁的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

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

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

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

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

主站蜘蛛池模板: 在线免费观看黄网 | 正在播放国产精品 | 视频一区二区三区四区五区 | 国产一区二区在线看 | 精品亚洲一区二区 | 国产探花在线精品一区二区 | 色综合激情| 51ⅴ精品国产91久久久久久 | 91中文字幕在线观看 | 亚洲成av人片在线观看无码 | 欧美成人一区二区三区片免费 | 成人欧美一区二区三区黑人孕妇 | 天天天天天操 | 国产欧美日韩 | 精品久久久久久亚洲精品 | 黄色片网站在线观看 | 亚洲国产成人久久综合一区,久久久国产99 | 欧美日日 | 国产精品美女久久久久久免费 | 第一区在线观看免费国语入口 | 亚洲精品一 | 伊人超碰在线 | 欧美日日 | 国产黄色精品在线观看 | 亚洲 中文 欧美 日韩 在线观看 | 精精久久 | 毛片免费视频 | 中文区中文字幕免费看 | 久久久久国产精品一区 | 日韩av黄色 | 成人a在线观看 | 在线免费看毛片 | 国产伦精品一区二区三毛 | xx性欧美肥妇精品久久久久久 | 欧美精品一二三 | 国产精品久久久久久久午夜片 | 亚洲国产成人在线观看 | 超碰人人爱| 成人精品一区亚洲午夜久久久 | 亚洲精品一区二区三区在线观看 | 一级黄片一级毛片 |