
ASP.NET AJAX的ReorderList控件是可以实现无排序数据绑定的列表控件,从名字上就可以看出来因为它是Reorder的,也就是说能够通过和服务器端交互数据重新排序绑定的数据项。要实现重排序,用户只需简单的拖动某条记录的可拖动部位,然后放到新的位置,在拖动记录的同时会有个图示化的显示。当某条记录被拖到新的地方时,数据源会被更新。
当ReorderList一旦绑定数据它就会像其它的数据绑定控件一样,当你设置了某一列的SortOrderField属性,那么它就会按照这一列的数据排序。ReorderList控件也可以绑定其它的IList控件(例如Arrays)。
ReorderList控件不同于其它的ASP.NET的AJAX控件,它是个服务器端的控件。所以ReorderList控件能够通过callback或postback来实现重排序。当某条记录被删除或者是编辑时,需要通过设置PostbackOnReorder属性来实现服务器端和客户端的数据同步
属性:
DataSourceID: 用来填充此控件的数据源。
. DataKeyField: 作为主键的数据字段。
SortOrderField: 排序字段。
ItemInsertLocation: 设定插入新的记录的位置,可以为Beginning或End。
DragHandleAlignment: 设定drag handle所在的位置。可以设置为Top, Left, Bottom, Right.
AllowReorder: 是否允许拖/放,默认是允许托/放的。
ItemTemplate - 该标签内将定义列表中普通条目的模版。 .
EditItemTemplate 该标签内将定义处于编辑状态中的列表条目的模版。
ReorderTemplate - 该标签内将定义拖动列表条目时列表中可投放区域的模版。
InsertItemTemplate - 该标签内将定义用来添加新条目的特殊行的模版。
DragHandleTemplate - 该标签内将定义列表条目中可拖放区域的模版。用户只有在该区域中拖拽才能够对该条目进行重排序。
EmptyListTemplate - 该标签内将定义空列表的模版。若列表中没有任何条目,则将显示出该模版中定义的内容。
PostbackOnReorder - 若设置该属性值为true,则当用户对列表中的项目进行重新排序之后,将自动引发一次整页的回送。否则将以异步回调的方式向服务器端发送请求。
一.Web.config代码:
<connectionStrings>
<add name="SQLCONNECTIONSTRING" connectionString="data Source=lxhslmlxh; database=testcombox; user id=sa;pwd=lxhslmlz"
providerName="SqlClient" />
<add name="TestcomboxConnectionString" connectionString="Data Source=LXHSLMLXH;Initial Catalog=Testcombox;Persist Security Info=True;User ID=sa;Password=lxhslmlz"
providerName="System.Data.SqlClient" />
connectionStrings>
二.前台default.aspx代
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页title>
<style type="text/css">
.DragHandleClass
{
width: 20px;
height: 12px;
background-color: red;
cursor:move;
}
style>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="width: 648px">
<cc1:ReorderList ID="ReorderList1" runat="server" AllowReorder="True"
PostBackOnReorder="False" SortOrderField="OrderID"
BorderStyle="Double" OnItemReorder="lxhslm" DataKeyField="ChaxunID" >
<DragHandleTemplate>
<div class="DragHandleClass">
div>
DragHandleTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"Content") %> || <%#DataBinder.Eval(Container.DataItem,"OrderID") %>
ItemTemplate>
cc1:ReorderList>
div>
ContentTemplate>
asp:UpdatePanel>
form>
body>
html>
三.后台default.aspx.cs代码
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Script.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bindinfo();
}
}
//获取前台信息
protected void lxhslm(object sender,AjaxControlToolkit.ReorderListItemReorderEventArgs e)
{
DataSet ds = Getinfo();
int nLength = Math.Abs(e.OldIndex - e.NewIndex);
int nBegin = -1;
if (e.OldIndex > e.NewIndex)
{
nBegin = e.NewIndex;
}
else
{
nBegin = e.OldIndex;
}
Updateorder(Int32.Parse(ds.Tables[0].Rows[e.OldIndex]["ChaxunID"].ToString()),Int32.Parse(ds.Tables[0].Rows[e.NewIndex]["OrderID"].ToString()));
for (int i = nBegin; i < nLength ; i++)
{
Updateorder(Int32.Parse(ds.Tables[0].Rows[i]["ChaxunID"].ToString()),Int32.Parse(ds.Tables[0].Rows[i]["OrderID"].ToString())-1);
}
}
//绑定数据
private void Bindinfo()
{
DataSet ds=Getinfo();
ReorderList1.DataSource = ds.Tables["aa"].DefaultView;
ReorderList1.DataBind();
}
//修改排序字段
private void Updateorder(int nChaxunID, int nNeworder)
{
SqlConnection myConnection = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
string cmdText = "UPDATE Mychaxun SET rderID=" + nNeworder + " WHERE ChaxunID=" + nChaxunID;
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
myConnection.Close();
}
}
//打开数据库
public DataSet Getinfo()
{
SqlConnection myConnection = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
string cmdText = "SELECT * FROM Mychaxun ORDER BY OrderID";
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
DataSet ds = new DataSet();
try
{
myConnection.Open();
da.Fill(ds, "aa");
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
myConnection.Close();
}
return ds;
}
}
五,运行结果