2个 listbox 互相调换数据(人员)

来源:互联网 发布:30w买车还是买房知乎 编辑:程序博客网 时间:2024/05/16 05:17

image

 

实现功能,对人员的预览调换。

点左,右按钮时不更改人员,点修改时做最后更改!

  

源视图代码:

view source
print?
02<table>
03    <tr>
04        <td>
05            <asp:ListBox ID="ListBox1" runat="server" Height="200px" Width="150px" 
06                SelectionMode="Multiple"></asp:ListBox></td>
07            <td width='1'>
08                <asp:Button ID="Button1" runat="server" Text="&gt;&gt;" OnClick="Button1_Click" />
09                <asp:Button ID="Button2" runat="server" Text="&lt;&lt;" OnClick="Button2_Click" />
10            </td>
11            <td>
12            <asp:ListBox ID="ListBox2" runat="server" Height="200px" Width="150px" 
13                    SelectionMode="Multiple"></asp:ListBox>
14        </td>
15    </tr>
16    <tr>
17        <td colspan="3" align="right"><asp:Button ID="btn" runat="server" Text="修改" 
18                onclick="btn_Click" /></td>
19    </tr>
20</table>

后台代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Left();
            Right();
        }
    }
    /// <summary>
    /// 右侧
    /// </summary>
    private void Right()
    {
        DbHelper db = new DbHelper();
        StringBuilder s = new StringBuilder();
        s.Append("select  * from account where gradeid in (2,3)");

        DataTable dt = db.GetDataTable(s.ToStr());
        this.ListBox2.DataSource = dt;
        this.ListBox2.DataTextField = "accountName";
        this.ListBox2.DataValueField = "accountid";
        this.ListBox2.DataBind();
        ViewState["dtR"] = dt;//将右侧数据放到页面临时变量中

    }

    /// <summary>
    /// 左侧
    /// </summary>
    private void Left()
    {
        DbHelper db = new DbHelper();
        StringBuilder s = new StringBuilder();
        s.Append("select  * from account where gradeid=1");

        DataTable dt = db.GetDataTable(s.ToStr());
        this.ListBox1.DataSource = dt;
        this.ListBox1.DataTextField = "accountName";
        this.ListBox1.DataValueField = "accountid";
        this.ListBox1.DataBind();
        ViewState["dtL"] = dt;//将左侧数据放到页面临时变量中

    }


    /// <summary>
    /// 左向右移动
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        //向右移动
        if (this.ListBox1.SelectedIndex >= 0)
        {
            //选中的数据,用来让listbox做增加删除。(当遍历一个listbox集合时做删除的话会影响该items项的大小,会报索引值错误)
            ArrayList ar = new ArrayList();
            //选中的数据,最后需要用来与右侧数据判断是否是左侧数据 
            StringBuilder s = new StringBuilder();

            //将选中项放到临时变量
            foreach (ListItem li in this.ListBox1.Items)
            {
                if (li.Selected)
                {
                    s.Append(li.Value + ",");
                    ar.Add(li);
                }
            }

            //执行右添加,左删除选中的数据
            foreach (ListItem li in ar)
            {
                this.ListBox2.Items.Add(li);//右添加
                this.ListBox1.Items.Remove(li);//左删除
            }

            if (ViewState["sR"].IsNotNullOrEmpty())
            {
                ViewState["sR"] = s.Append(ViewState["sR"]);
            }
            else
            {
                ViewState["sR"] = s;//右临时变量
            }

        }
    }

    /// <summary>
    /// 右向左移动
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        //保存在左临时变量中

        //向左移动
        if (this.ListBox2.SelectedIndex > 0)
        {
            ArrayList al = new ArrayList();
            StringBuilder s = new StringBuilder();
            //移动到左侧
            foreach (ListItem li in this.ListBox2.Items)
            {
                if (li.Selected)
                {
                    s.Append(li.Value);
                    al.Add(li);
                }
            }

            //执行左添加,右删除选中的数据
            foreach (ListItem li in al)
            {
                this.ListBox2.Items.Remove(li);//右删除
                this.ListBox1.Items.Add(li);//左添加
            }

            if (ViewState["sL"].IsNotNullOrEmpty())
            {
                ViewState["sL"] = s.Append(ViewState["sL"]);
            }
            else
            {
                ViewState["sL"] = s;//左临时变量
            }

        }

    }

    /// <summary>
    /// 修改組成員
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Click(object sender, EventArgs e)
    {
        //处理左临时变量
        GetLeftTemp();
        //处理右临时变量
        GetRightTemp();
    }

    /// <summary>
    /// 获取左临时变量与左侧数据比较
    /// </summary>
    private void GetLeftTemp()
    {
        if (ViewState["sL"].IsNotNullOrEmpty() && ViewState["dtL"].IsNotNullOrEmpty())
        {
            bool isOpen = true;//默认没有相同值
            StringBuilder sEdit = new StringBuilder();//选中值的结果

            string sL = ViewState["sL"].ToString();
            string[] sTemp = sL.Trim(',').Split(',');
            DataTable dt = (DataTable)ViewState["dtL"];
            foreach (string s in sTemp)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int drTemp = dr["accountId"].ToInt();
                    int iTemp = s.ToInt();
                    if (iTemp == drTemp)//在这里判断如果选中的左临时变量 sL 与 dtL 相同那么..
                    {
                        isOpen = false;//有相同值
                    }
                }
                if (isOpen)//当没有相同值的时候存储要修改的ID
                {
                    //执行这里的操作(与下面的获取右临时变量相同)
                    sEdit.Append(s + ",");
                }
            }
        }
    }

    /// <summary>
    /// 获取右临时变量与右侧数据比较
    /// </summary>
    private void GetRightTemp()
    {
        if (ViewState["sR"].IsNotNullOrEmpty() && ViewState["dtR"].IsNotNullOrEmpty())
        {
            bool isOpen = true;
            StringBuilder sEdit = new StringBuilder();

            string sR = ViewState["sR"].ToString();
            string[] sTemp = sR.Trim(',').Split(',');
            DataTable dt = (DataTable)ViewState["dtR"];
            foreach (string s in sTemp)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int iTemp = s.ToInt();
                    int drTemp = dr["accountid"].ToInt();
                    if (iTemp != drTemp)
                    {
                        string st = string.Empty;
                    }
                    else
                    {
                        isOpen = false;
                    }
                }
                if (isOpen)
                {
                    sEdit.Append(s + ",");
                }
            }
        }

    }

 

 

原创粉丝点击