aspx页面代码:
<%#(Convert.ToInt32(Eval("sortindex")) == nMaxIndex) ? "" : "<td class='wenzi'> <a href='#' onclick=\"sortInformation(" + Eval("fid") + "," + ",'up'," + Eval("sortindex") + ")\">上移</a></td>"%> <%#(Convert.ToInt32(Eval("sortindex")) == nMinIndex) ? "" : "<td class='wenzi'> <a href='#' onclick=\"sortInformation(" + Eval("fid") + "," + ",'down'," + Eval("sortindex") + ")\">下移</a></td>"%>
上述三元表达式,保证当是列表中第一条数据时,没有上移链接,当是列表中最后一条数据时候,没有下移链接;
aspx.cs文件代码:
protected int nMaxIndex = FooterBLL.GetMaxIndex(); //获得最大索引 protected int nMinIndex = FooterBLL.GetMinIndex(); //获得最小索引
JS文件代码:
//上下移动 function sortInformation(keyID,optype,sortindex) { if (keyID <= 0) { alert("无效的信息ID参数,操作终止。"); return; } if (sortindex <= 0) { alert("无效的索引值,操作终止。"); return; } if (optype != ‘up’ && optype != 'down') { alert("无效的操作类型,操作终止。"); return; } $.ajax({ type: 'POST', url: 'UserControl/SetInfoSort.ashx', data: { KeyID: keyID, SortIndex: sortindex, OpType: optype }, success: function (res) { if (res == 1) { window.location.reload(); } else { alert("移动操作失败,请重试。"); } }, error: function () { alert("移动请求发送失败,请重试。"); } }); }
ashx文件代码:
public class SetInfoSort : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string strRescult = "0"; //1:成功 0:失败 string strKeyID = context.Request.Form["KeyID"]; int nKeyID = 0; if (!int.TryParse(strKeyID, out nKeyID) || nKeyID <= 0) { context.Response.Write(strRescult); return; } string strSortIndex = context.Request.Form["SortIndex"]; int nSortIndex = 0; if (!int.TryParse(strSortIndex, out nSortIndex) || nSortIndex <= 0) { context.Response.Write(strRescult); return; } string strCateID = context.Request.Form["CateID"]; int nCateID = 0; if (!int.TryParse(strCateID, out nCateID) || nCateID <= 0) { context.Response.Write(strRescult); return; } string strOpType = context.Request.Form["OpType"];
if(strOpType != "up" && strOpType != "down") { context.Response.Write(strRescult); return; } if (InformationBLL.SetInformationSort(nKeyID,nSortIndex,nOpType)) strRescult = "1"; context.Response.Write(strRescult); } public bool IsReusable { get { return false; } } }
SQL存储过程:
ALTER PROCEDURE [dbo].[Footer_Sort] @UPKEYID INT, --改变排序之前,序号较大者 @DOWNKEYID INT, --改变排序之前,序号较小者 @RESCULT INT OUTPUT --1:成功 0:失败 AS BEGIN SET @RESCULT = 0 BEGIN TRAN T DECLARE @TEMPUP INT DECLARE @TEMPDOWN INT SELECT @TEMPUP = SORTINDEX FROM FOOTER WHERE FID = @UPKEYID SELECT @TEMPDOWN = SORTINDEX FROM FOOTER WHERE FID = @DOWNKEYID UPDATE FOOTER SET SORTINDEX = @TEMPDOWN WHERE FID = @UPKEYID IF(@@ERROR <> 0) BEGIN ROLLBACK TRAN T END UPDATE FOOTER SET SORTINDEX = @TEMPUP WHERE FID = @DOWNKEYID IF(@@ERROR <> 0) BEGIN ROLLBACK TRAN T END SET @RESCULT = 1 COMMIT TRAN T END