ListBox控件的使用

来源:互联网 发布:专家网络行业 知乎 编辑:程序博客网 时间:2024/05/16 09:31

 该实例的功能是使用左边的ListBox控件向用户显示预定义选项列表,单击向右按钮,将用户选择的项添加到右边的ListBox控件中,单击向左的按钮,将右边的ListBox控件中用户选择的项添加到左边的ListBox控件中。

以下是源代码:

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.Data.SqlClient;

public partial class listbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                DataSet ds = new DataSet();
                SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=860712;database=Northwind");
                string strSQL = "select name from sysColumns where id=2073058421";
                SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
                myCommand.Fill(ds, "Customers");

                ListBox1.DataSource = ds.Tables["Customers"].DefaultView;
                ListBox1.DataValueField = ds.Tables["Customers"].Columns[0].ColumnName;
                ListBox1.DataTextField = ds.Tables["Customers"].Columns[0].ColumnName;
                ListBox1.DataBind();
            }
            catch
            {
                Response.Write("<script language='javascript'>alert('连接失败!')<script>");
            }
        }
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ListBox1.Items.Count != 0)
        {
            if (ListBox1.SelectedIndex > -1)
            {
                //单选的代码
                //ListBox2.Items.Add(ListBox1.SelectedItem);
                //ListBox1.Items.Remove(ListBox1.SelectedItem);
                //ListBox2.ClearSelection();
                //多选的代码
                for (int i = ListBox1.Items.Count - 1; i > -1; i--)
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        ListBox2.Items.Add(ListBox1.Items[i]);
                        ListBox1.Items.Remove(ListBox1.Items[i]);
                        ListBox2.ClearSelection();
                    }

                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('你没有选中左边的列表项!')</script>");
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ListBox2.Items.Count != 0)
        {
            if (ListBox2.SelectedIndex > -1)
            {
                for (int j = ListBox2.Items.Count - 1; j > -1; j--)
                {
                    if (ListBox2.Items[j].Selected)
                    {
                        ListBox1.Items.Add(ListBox2.Items[j]);
                        ListBox2.Items.Remove(ListBox2.Items[j]);
                        ListBox1.ClearSelection();
                    }
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('你没有选中右边的列表项!')</script>");
            }
        }
    }
}

原创粉丝点击