asp.net中使用 Repeater控件拖拽實(shí)現(xiàn)排序并同步數(shù)據(jù)庫(kù)字段排序
數(shù)據(jù)庫(kù)表中有一個(gè)單位表,里面包括ID、Name、Order等字段,現(xiàn)在有個(gè)后臺(tái)管理功能,可以設(shè)置這些單位在某些統(tǒng)計(jì)表格中的先后顯示順序,于是想到用拖拽方式實(shí)現(xiàn),這樣操作起來(lái)更簡(jiǎn)便。
使用了GifCam軟件做了一個(gè)示例動(dòng)畫(huà),效果如下圖所示:

于是就動(dòng)手起來(lái),發(fā)現(xiàn)jquery.ui中提供sortable函數(shù),可用于排序,界面中從數(shù)據(jù)庫(kù)綁定的單位使用Repeater控件,下面簡(jiǎn)單介紹下主要步驟:
1、項(xiàng)目中使用到的jquery-1.7.2.min.js和jquery-ui.min.js請(qǐng)點(diǎn)擊進(jìn)行下載,地址為:http://download.csdn.net/detail/taomanman/9315373
2、TestDemo.aspx代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../Scripts/jquery-1.7.2.min.js"></script>
<script src="../../Scripts/jquery-ui.min.js"></script>
<title>Repeater拖拽排序</title>
<style type="text/css">
#module_list {
margin-left: 4px;
}
.modules {
float: left;
width: 200px;
height: 140px;
margin: 10px;
border: 1px solid #acc6e9;
background: #e8f5fe;
}
.m_title {
margin-top: 0px;
height: 24px;
line-height: 24px;
background: #afc6e9;
}
#loader {
height: 24px;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="loader"></div>
<div id="module_list">
<input type="hidden" id="orderlist" />
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<div class="modules" title='<%#Eval("F_DataCenterID") %>'>
<h3 class="m_title"><%#Eval("F_DataCenterName").ToString() %></h3>
<p><%#Eval("F_Order") %></p>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
<script type="text/javascript">
$(function () {
$(".m_title").bind('mouseover', function () {
$(this).css("cursor", "move")
});
var show = $("#loader");
var orderlist = $("#orderlist");
var list = $("#module_list");
var old_order = [];
//獲取原先的順序列表
list.children(".modules").each(function () {
var val = $(this).find("p").text();
old_order.push(val);
});
list.sortable({
opacity: 0.6, //設(shè)置拖動(dòng)時(shí)候的透明度
revert: true, //緩沖效果
cursor: 'move', //拖動(dòng)的時(shí)候鼠標(biāo)樣式
handle: '.m_title', //可以拖動(dòng)的部位,模塊的標(biāo)題部分
update: function () {
var new_id = [];
list.children(".modules").each(function () {
new_id.push(this.title);
});
var newid = new_id.join(',');
var oldid = old_order.join(',');
$.ajax({
type: "post",
url: "update.aspx", //服務(wù)端處理程序
data: { id: newid, order: oldid }, //id:新的排列對(duì)應(yīng)的ID,order:原排列順序
beforeSend: function () {
show.html("<img src='load.gif' /> 正在更新...");
},
success: function (msg) {
show.html("排序成功...");
//重新刷新頁(yè)面
window.location.reload();
}
});
}
});
});
</script>
TestDemo.cs代碼如下,具體數(shù)據(jù)庫(kù)操作類獲取數(shù)據(jù)根據(jù)各自的情況進(jìn)行,這里就不詳細(xì)介紹了。
public partial class TestDemo : System.Web.UI.Page
{
public static GGJ_DC_DataCenterBaseInfoBLL bll = new GGJ_DC_DataCenterBaseInfoBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
/// <summary>
/// 綁定部委單位
/// </summary>
public void BindData()
{
string where = "";
string orderby = "F_Order ASC";
DataTable dt = bll.GetData(where, orderby);
this.rpt.DataSource = dt;
this.rpt.DataBind();
}
}
3、$.ajax方法請(qǐng)求的頁(yè)面update.aspx及update.aspx.cs代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
[csharp] view plaincopy
public partial class update : System.Web.UI.Page
{
public static GGJ_DC_DataCenterBaseInfoBLL bll = new GGJ_DC_DataCenterBaseInfoBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string order = Request["order"].ToString();
string depId = Request["id"].ToString();
UpdateOrder(depId, order);
}
}
/// <summary>
/// 重新更新順序
/// </summary>
/// <param name="deptId"></param>
/// <param name="order"></param>
public void UpdateOrder(string deptId, string order)
{
string[] deptIds = deptId.Split(',');
string[] orders = order.Split(',');
for (int i = 0; i < deptIds.Length; i++)
{
for (int j = 0; j < orders.Length; j++)
{
if (i == j)
{
string sql = "update GGJ_DC_DataCenterBaseInfo set F_Order=" + orders[j] + " where F_DataCenterID='" + deptIds[i]+ "'";
DataTable dt = CommonClass.QuerySQL.GetDataTable(sql);
if (dt.Rows.Count > 0)
{
}
}
}
}
}
}
以上內(nèi)容是小編給大家介紹的關(guān)于asp.net中使用 Repeater控件拖拽實(shí)現(xiàn)排序并同步數(shù)據(jù)庫(kù)字段排序的相關(guān)敘述,希望大家喜歡。
- 詳解ASP.NET-----Repeater數(shù)據(jù)控件的用法總結(jié)
- 詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法
- ASP.NET數(shù)據(jù)綁定之Repeater控件
- asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
- ASP.NET中repeater控件用法實(shí)例
- asp.net Repeater控件的說(shuō)明及詳細(xì)介紹及使用方法
- asp.net下Repeater使用 AspNetPager分頁(yè)控件
- asp.net 遍歷repeater中的控件的幾種方式
- ASP.NET實(shí)現(xiàn)Repeater控件的數(shù)據(jù)綁定
相關(guān)文章
IIS7中ASP.net 請(qǐng)求處理過(guò)程說(shuō)明
IIS7 站點(diǎn)啟動(dòng)并處理請(qǐng)求的步驟如下,在iis7中處理asp.net的朋友可以參考下。2011-02-02
MVC+EasyUI+三層新聞網(wǎng)站建立 主頁(yè)布局的方法(五)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第五篇,教大家如何進(jìn)行主頁(yè)布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
asp.net 操作cookie的簡(jiǎn)單實(shí)例
這篇文章主要介紹了asp.net 操作cookie的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12
asp.net(c#)開(kāi)發(fā)中的文件上傳組件uploadify的使用方法(帶進(jìn)度條)
在asp.net開(kāi)發(fā)中,有很多可以上傳的組件模塊,利用HTML的File控件(uploadify)的上傳也是一種辦法,這里為大家介紹一下(uploadify)的一些使用方法2012-12-12
在SQL Server中使用CLR調(diào)用.NET方法實(shí)現(xiàn)思路
在.NET中新建一個(gè)類,并在這個(gè)類里新建一個(gè)方法,然后在SQL Server中調(diào)用這個(gè)方法,接下來(lái)我們將實(shí)現(xiàn)這個(gè)功能做了以下幾個(gè)步驟,詳細(xì)看下本文,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02
理解HttpHandler,并為所有*.jpg圖片生成一段文字于圖片上
HttpHandler就是最終相應(yīng)HTTP請(qǐng)求,生成HTTP響應(yīng)的處理器,他們的實(shí)例由asp.net運(yùn)行時(shí)創(chuàng)建,,并生存在asp.net的運(yùn)行時(shí)環(huán)境中,如果asp.net運(yùn)行時(shí)是處理請(qǐng)求的工廠,HttpHandler是處理請(qǐng)求的工人2012-03-03

