• Repeater上下排序按钮


    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" />
                                        &nbsp;
                                        <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>
                                        &nbsp;
                                        <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和排序,然后同时修改就行了

  • 相关阅读:
    Freemaker Replace函数的正则表达式运用
    [Android]使用Spring for Android改善数据交互流程
    [Freemarker]自定义时间戳函数
    [jquery.validate]自定义方法实现"手机号码或者固定电话"的逻辑验证
    [Javascript]网页链接加上时间戳防止串用户
    支付系统设计
    java设计模式-抽象工厂模式
    IDEA解决maven多module出现多root的问题
    java设计模式-工厂方法模式
    java设计模式-单例模式
  • 原文地址:https://www.cnblogs.com/webapi/p/5718445.html
Copyright © 2020-2023  润新知