轉(zhuǎn)帖|其它|編輯:郝浩|2011-01-13 14:47:42.000|閱讀 757 次
概述:右鍵菜單非常方便,很多時(shí)候會用到。這篇文章將使用一個(gè)JQUERY的插件在ASP.NET MVC中實(shí)現(xiàn)右鍵菜單。本文還將介紹一下在ASP.NET MVC中如何實(shí)現(xiàn)簡單的分頁。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
右鍵菜單非常方便,很多時(shí)候會用到。這篇文章將使用一個(gè)JQUERY的插件在ASP.NET MVC中實(shí)現(xiàn)右鍵菜單。本文還將介紹一下在ASP.NET MVC中如何實(shí)現(xiàn)簡單的分頁。效果如下圖:
首先,下載此插件。
新建一個(gè)asp.net mvc應(yīng)用程序。將此插件放入Scripts文件夾。并在頁面上引用。
這個(gè)demo使用到NORTHWND數(shù)據(jù)庫的Product表。
定義右鍵菜單:
<div class="contextMenu" id="myMenu1"> <ul>
<li id="detail">
<img src="//www.cnblogs.com/Content/detail.ico" />detail</li>
<li id="new"><img src="//www.cnblogs.com/Content/new.ico" />new</li>
<li id="delete">
<img src="//www.cnblogs.com/Content/delete.ico"/>delete</li>
<li id="modify">
<img src="//www.cnblogs.com/Content/modify.ico"/>modify</li>
</ul> </div>
將此菜單定義在產(chǎn)品名上,故在在產(chǎn)品名上添加一個(gè)class供jquery選擇。
<td class="showContext" id="<%= item.ProductID %>">
<%: item.ProductName %></td>
在頁面上插入下面腳本。用于綁定菜單項(xiàng)的行為。為了簡單起見,將所以的菜單項(xiàng)的行為都定義成導(dǎo)航到詳情頁面.
<script type="text/javascript">
$(document).ready(function () {
$('td.showContext').contextMenu('myMenu1', {
bindings: {
'detail': function (t) {
document.location.href = '/Products/Detail/'+t.id;
},
'new': function (t) {
document.location.href = '/Products/Detail/' + t.id;
},
'delete': function (t) {
confirm("你確定刪除嗎?");
document.location.href = '/Products/Detail/' + t.id;
},
'modify': function (t) {
document.location.href = '/Products/Detail/' + t.id;
}
}
});
});
</script>
這樣就非常簡單的實(shí)現(xiàn)了右鍵菜單的功能。
下面說下實(shí)現(xiàn)簡單的分頁。asp.net mvc中分頁非常簡單。
看下面定義的table的html代碼:
<table>
<tr>
<th>
ProductName
</th>
<th>
SupplierID
</th>
<th>
CategoryID11 </th>
<th>
QuantityPerUnit
</th>
<th>
UnitPrice
</th>
<th>
UnitsInStock20 </th>
<th>
UnitsOnOrder23 </th>
<th>
ReorderLevel
</th>
<th>
Discontinued
</th>
</tr>
<% foreach (var item in Model.Products)
{ %>
<tr>
<td class="showContext" id="<%= item.ProductID %>">
<%: item.ProductName %></td>
<td>
<%: item.SupplierID %>
</td>
<td>
<%: item.CategoryID %>
</td>
<td>
<%: item.QuantityPerUnit %>
</td>
<td>
<%: String.Format("{0:F}", item.UnitPrice) %>
</td>
<td>
<%: item.UnitsInStock %>
</td>
<td>
<%: item.UnitsOnOrder %>
</td>
<td>
<%: item.ReorderLevel %>
</td>
<td>
<%: item.Discontinued %>
</td>
</tr>
<% } %>
</table>
我們只要在這個(gè)table下面插入一段分頁的HTML腳本就行了。分頁的腳本當(dāng)然要生成,使用Htmlhelper的擴(kuò)展方法去生成這個(gè)腳本。看下面的擴(kuò)展方法,非常的簡單的生成了分頁的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("<a href=\"{0}/{1}\">Previous</a>", urlPrefix, currentPage));
if (currentPage - currentPageSize >= 0)
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));
for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)
{
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", urlPrefix, i + 1));
}
if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));
if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", urlPrefix, currentPage + 2));
return sb1.ToString();
}
document.location.href = '/Products/Detail/' + t.id;
},
'delete': function (t) {
confirm("你確定刪除嗎?");
document.location.href = '/Products/Detail/' + t.id;
},
'modify': function (t) {
document.location.href = '/Products/Detail/' + t.id;
}
}
});
});
</script>
然后在table后面添加下面的代碼,在table下面輸出分頁的html代碼:
<div class="pager">
<%=Html.Pager(Model.CurrentPage, Model.TotalPages,Model.TotalItems ,"/Products/List")%>
</div>
這樣就完成分頁和右鍵菜單的功能了。是不是非常的簡單呢。:)
效果:
顯示:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載