自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET MVC 2中實現(xiàn)右鍵菜單和簡單分頁

開發(fā) 后端
在這里我們將討論的是通過一個插件實現(xiàn)ASP.NET MVC 2中的右鍵菜單和一個相當簡單的分頁,希望對大家有所幫助。

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

首先,下載此插件

新建一個asp.net mvc應(yīng)用程序。將此插件放入Scripts文件夾。并在頁面上引用。

這個demo使用到NORTHWND數(shù)據(jù)庫的Product表。

定義右鍵菜單:

  1. <div class="contextMenu" id="myMenu1"> <ul>   
  2. <li id="detail">
  3. <img src="http://www.cnblogs.com/Content/detail.ico" />detail</li>   
  4. <li id="new"><img src="http://www.cnblogs.com/Content/new.ico" />new</li>
  5. <li id="delete"> 
  6. <img src="http://www.cnblogs.com/Content/delete.ico"/>delete</li>   
  7. <li id="modify">
  8. <img src="http://www.cnblogs.com/Content/modify.ico"/>modify</li>   
  9.  </ul> </div> 

將此菜單定義在產(chǎn)品名上,故在在產(chǎn)品名上添加一個class供jquery選擇。

  1. <td class="showContext" id="<%= item.ProductID %>">
  2. <%: item.ProductName %></td> 

在頁面上插入下面腳本。用于綁定菜單項的行為。為了簡單起見,將所以的菜單項的行為都定義成導(dǎo)航到詳情頁面.

  1. <script type="text/javascript">   
  2.    $(document).ready(function () {   
  3.       $('td.showContext').contextMenu('myMenu1', {   
  4.          bindings: {  
  5.                'detail'function (t) {   
  6.            document.location.href = '/Products/Detail/'+t.id;   
  7.                 },   
  8.                'new'function (t) {  
  9.          document.location.href = '/Products/Detail/' + t.id;  
  10.               },  
  11.                  'delete'function (t) {  
  12.                      confirm("你確定刪除嗎?");  
  13.           document.location.href = '/Products/Detail/' + t.id;  
  14.                 },  
  15.                  'modify'function (t) {  
  16.        document.location.href = '/Products/Detail/' + t.id;  
  17.                }  
  18.              }  
  19.         });  
  20.      });  
  21. </script> 

這樣就非常簡單的實現(xiàn)了右鍵菜單的功能。

下面說下實現(xiàn)簡單的分頁。asp.net mvc中分頁非常簡單。

看下面定義的table的html代碼:

  1.  <table>   
  2.   <tr>   
  3.             <th>   
  4.                 ProductName   
  5.              </th> 
  6.           <th>   
  7.                SupplierID   
  8.             </th>   
  9.             <th> 
  10.                CategoryID11             </th> 
  11.             <th> 
  12.                  QuantityPerUnit  
  13.           </th> 
  14.             <th> 
  15.                  UnitPrice  
  16.            </th> 
  17.              <th> 
  18.                 UnitsInStock20             </th> 
  19.            <th> 
  20.                  UnitsOnOrder23             </th> 
  21.              <th> 
  22.                 ReorderLevel  
  23.             </th> 
  24.             <th> 
  25.                 Discontinued  
  26.              </th> 
  27.          </tr> 
  28.     <% foreach (var item in Model.Products)  
  29.         { %> 
  30.         <tr> 
  31.   <td class="showContext" id="<%= item.ProductID %>"> 
  32. <%: item.ProductName %></td> 
  33.              <td> 
  34.                  <%: item.SupplierID %> 
  35.            </td> 
  36.              <td> 
  37.                 <%: item.CategoryID %> 
  38.             </td> 
  39.              <td> 
  40.                 <%: item.QuantityPerUnit %> 
  41.              </td> 
  42.              <td> 
  43.        <%: String.Format("{0:F}", item.UnitPrice) %> 
  44.             </td> 
  45.              <td> 
  46.                 <%: item.UnitsInStock %> 
  47.              </td> 
  48.           <td> 
  49.              <%: item.UnitsOnOrder %> 
  50.              </td> 
  51.           <td> 
  52.            <%: item.ReorderLevel %> 
  53.             </td> 
  54.             <td> 
  55.                <%: item.Discontinued %> 
  56.           </td> 
  57.         </tr>      
  58.     <% } %> 
  59. </table> 

我們只要在這個table下面插入一段分頁的HTML腳本就行了。分頁的腳本當然要生成,使用Htmlhelper的擴展方法去生成這個腳本??聪旅娴臄U展方法,非常的簡單的生成了分頁的html代碼:

  1. public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords, string urlPrefix)   
  2.         {  
  3.            StringBuilder sb1 = new StringBuilder();   
  4. int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);   
  5. if (currentPage > 0)   
  6. sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Previous</a>", urlPrefix, currentPage));   
  7. if (currentPage - currentPageSize >= 0)  
  8. sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));  
  9. for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)  
  10.  {  
  11. sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", urlPrefix, i + 1));  
  12.  }  
  13. if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))  
  14. sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));  
  15. if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))  
  16. sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", urlPrefix, currentPage + 2));  
  17. return sb1.ToString();  

然后在table后面添加下面的代碼,在table下面輸出分頁的html代碼:

 
  1. <div class="pager">   
  2. <%=Html.Pager(Model.CurrentPage, Model.TotalPages,Model.TotalItems ,"/Products/List")%>
  3.    </div> 

這樣就完成分頁和右鍵菜單的功能了。是不是非常的簡單呢。:)

效果:

效果

顯示:

右鍵菜單

如果有興趣可以下載代碼。

總結(jié):在asp.net mvc中實現(xiàn)右鍵菜單和簡單的分頁。

代碼http://cid-aef1e64945224a20.office.live.com/self.aspx/.Public/ContextMenuDemo.rar

原文標題:ASP.NET MVC2右鍵菜單和最簡單分頁

鏈接:http://www.cnblogs.com/zhuqil/archive/2010/08/01/asp-net-mvc-context-menu-and-simple-paging.html

【編輯推薦】

  1. 添加設(shè)置ASP.NET Web時出現(xiàn)問題
  2. 詳細說明ASP.NET 2.0功能支持
  3. 強化部署ASP.Net 2.0配置應(yīng)用程序
  4. 微軟PDC2009直擊:改進ASP.NET 4運行時
  5. 詳解ASP.NET MVC 2自定義驗證

 

 

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2009-07-28 14:47:18

ASP.NET MVC

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-09-10 09:50:47

ASP.NET MVC

2010-03-19 09:17:16

ASP.NET MVC

2012-04-13 10:05:24

ASP.NET

2009-07-20 15:44:32

ASP.NET MVC

2010-02-03 09:50:58

ASP.NET MVC

2010-01-26 13:15:42

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-12-21 10:05:10

ASP.NET MVC

2009-07-31 12:43:59

ASP.NET MVC

2009-04-20 09:43:37

ASP.NET MVC基礎(chǔ)開發(fā)

2012-04-23 15:10:18

ASP.NET

2010-10-14 09:05:36

ASP.NET MVC

2010-11-02 08:46:55

NupackASP.NET MVC

2009-06-12 09:24:34

ASP.NET窗體ASP.NET MVC

2009-07-22 13:16:04

MvcAjaxPaneASP.NET MVC

2010-02-05 08:32:32

ASP.NET MVC

2009-09-24 09:26:22

ASP.NET MVC

2009-09-09 09:09:17

ASP.NET MVC
點贊
收藏

51CTO技術(shù)棧公眾號