新聞中心
右鍵菜單非常方便,很多時候會用到。這篇文章將使用一個JQUERY的插件在ASP.NET MVC中實現(xiàn)右鍵菜單。本文還將介紹一下在ASP.NET MVC中如何實現(xiàn)簡單的分頁。效果如下圖:

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供冀州企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、網(wǎng)站制作、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為冀州眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
首先,下載此插件。
新建一個asp.net mvc應(yīng)用程序。將此插件放入Scripts文件夾。并在頁面上引用。
這個demo使用到NORTHWND數(shù)據(jù)庫的Product表。
定義右鍵菜單:
detail
new
delete
modify
將此菜單定義在產(chǎn)品名上,故在在產(chǎn)品名上添加一個class供jquery選擇。
"> - <%: item.ProductName %>
在頁面上插入下面腳本。用于綁定菜單項的行為。為了簡單起見,將所以的菜單項的行為都定義成導(dǎo)航到詳情頁面.
這樣就非常簡單的實現(xiàn)了右鍵菜單的功能。
下面說下實現(xiàn)簡單的分頁。asp.net mvc中分頁非常簡單。
看下面定義的table的html代碼:
- ProductName
- SupplierID
- CategoryID11
- QuantityPerUnit
- UnitPrice
- UnitsInStock20
- UnitsOnOrder23
- ReorderLevel
- Discontinued
- <% foreach (var item in Model.Products)
- { %>
"> - <%: item.ProductName %>
- <%: item.SupplierID %>
- <%: item.CategoryID %>
- <%: item.QuantityPerUnit %>
- <%: String.Format("{0:F}", item.UnitPrice) %>
- <%: item.UnitsInStock %>
- <%: item.UnitsOnOrder %>
- <%: item.ReorderLevel %>
- <%: item.Discontinued %>
- <% } %>
我們只要在這個table下面插入一段分頁的HTML腳本就行了。分頁的腳本當(dāng)然要生成,使用Htmlhelper的擴展方法去生成這個腳本??聪旅娴臄U展方法,非常的簡單的生成了分頁的html代碼:
- public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords, string urlPrefix)
- {
- StringBuilder sb1 = new StringBuilder();
- int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);
- if (currentPage > 0)
- sb1.AppendLine(String.Format("Previous", urlPrefix, currentPage));
- if (currentPage - currentPageSize >= 0)
- sb1.AppendLine(String.Format("...", urlPrefix, (currentPage - currentPageSize) + 1));
- for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)
- {
- sb1.AppendLine(String.Format("{1}", urlPrefix, i + 1));
- }
- if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
- sb1.AppendLine(String.Format("...", urlPrefix, (currentPage + currentPageSize) + 1));
- if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
- sb1.AppendLine(String.Format("Next", urlPrefix, currentPage + 2));
- return sb1.ToString();
- }
然后在table后面添加下面的代碼,在table下面輸出分頁的html代碼:
- <%=Html.Pager(Model.CurrentPage, Model.TotalPages,Model.TotalItems ,"/Products/List")%>
這樣就完成分頁和右鍵菜單的功能了。是不是非常的簡單呢。:)
效果:
顯示:
如果有興趣可以下載代碼。
總結(jié):在asp.net mvc中實現(xiàn)右鍵菜單和簡單的分頁。
代碼:http://cid-aef1e64945224a20.office.live.com/self.aspx/.Public/ContextMenuDemo.rar
網(wǎng)頁標(biāo)題:ASP.NET MVC 2中實現(xiàn)右鍵菜單和簡單分頁
文章轉(zhuǎn)載:http://m.fisionsoft.com.cn/article/cosoioi.html


咨詢
建站咨詢
