aspx代码
<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0" class="tdh"> <tr class="tabletda"> <td style="35%"> <strong>栏目名称</strong> </td> <td style="35%"> <strong>排序</strong> </td> <td> <strong>操作</strong></td> </tr> <asp:Repeater ID="ReptType" runat="server" OnItemDataBound="ReptType_ItemDataBound"> <ItemTemplate> <tr class="tdbg" onmouseover="this.style.backgroundColor='#eff6fc'" onmouseout="this.style.backgroundColor=''"> <td style="height:25px;"> <%#Eval("lmname")%> </td> <td align="center"> <asp:HiddenField ID="zindex" runat="server" Value=<%# Container.ItemIndex%> /> <asp:ImageButton ID="linkUp" ImageUrl="images/up.png" CommandArgument='<%#Eval("ID")%>' CommandName='<%#Eval("xu")%>' runat="server" OnClick="lbUp_Click" /> <asp:ImageButton ID="linkDown" ImageUrl="images/down.png" CommandArgument='<%#Eval("ID")%>' CommandName='<%#Eval("xu")%>' runat="server" OnClick="lbDown_Click" /> </td> <td align="center"> <a href="lmrightAdd.aspx?cid=<%=Request.QueryString["cid"] %>&id=<%#Eval("ID")%>">修改</a> <asp:LinkButton ID="lbDel" runat="server" OnClick="lbDel_Click" OnClientClick="return confirm('您确认要删除吗?')" CommandArgument='<%#Eval("ID")%>' ForeColor="red">删除</asp:LinkButton> </td> </tr> </ItemTemplate> </asp:Repeater> </table>
cs代码:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;using System.Text; public partial class List : adminFace { public int datacount = 0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ReptDT(); } } /// <summary> /// 邦定类别数据 /// </summary> public void ReptDT() { string sql = "select * from dtname order by xu asc"; DataTable dt = Maticsoft.DBUtility.DbHelperSQL.Query(sql).Tables[0]; datacount = dt.Rows.Count; ReptType.DataSource = dt; ReptType.DataBind(); } /// <summary> /// 被绑定数据后触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ReptType_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { //上移 ImageButton linkUp = e.Item.FindControl("linkUp") as ImageButton; ImageButton linkDown = e.Item.FindControl("linkDown") as ImageButton; if (e.Item.ItemIndex == 0) { linkUp.Enabled = false; linkUp.ImageUrl = "images/up0.png"; } if (e.Item.ItemIndex == datacount - 1) { linkDown.Enabled = false; linkDown.ImageUrl = "images/down0.png"; } } } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lbDel_Click(object sender, EventArgs e) { int cid = MyCLib.StrClass.ConvertToInt(((LinkButton)sender).CommandArgument, 0); //删除代码就不写了 } //上移 protected void lbUp_Click(object sender, EventArgs e) { ImageButton button = (ImageButton)sender; HiddenField hf = (HiddenField)button.NamingContainer.FindControl("zindex"); ImageButton linkOther = (ImageButton)ReptType.Items[int.Parse(hf.Value)-1].FindControl("linkUp"); //先修改自己,然后修改上一个 StringBuilder sb = new StringBuilder(); sb.Append("update dt_lmright set xu=" + linkOther.CommandName + " where ID=" + button.CommandArgument + ";"); sb.Append("update dt_lmright set xu=" + button.CommandName + " where ID=" + linkOther.CommandArgument + ";"); if (Maticsoft.DBUtility.DbHelperSQL.ExecuteSql(sb.ToString()) > 0) { ReptDT(); } } //下移 protected void lbDown_Click(object sender, EventArgs e) { ImageButton button = (ImageButton)sender; HiddenField hf = (HiddenField)button.NamingContainer.FindControl("zindex"); ImageButton linkOther = (ImageButton)ReptType.Items[int.Parse(hf.Value) + 1].FindControl("linkDown"); //先修改自己,然后修改下一个 StringBuilder sb = new StringBuilder(); sb.Append("update dt_lmright set xu=" + linkOther.CommandName + " where ID=" + button.CommandArgument + ";"); sb.Append("update dt_lmright set xu=" + button.CommandName + " where ID=" + linkOther.CommandArgument + ";"); if (Maticsoft.DBUtility.DbHelperSQL.ExecuteSql(sb.ToString()) > 0) { ReptDT(); } } }
原理:找到相邻行,然后获取ID和排序,然后同时修改就行了