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>");
}
}
}
}
- ListBox控件的使用
- ListBox控件的使用
- 控件listbox的使用
- VC++------------ListBox控件的使用
- VC++----ListBox控件的使用
- (转载)mfc listbox 控件-ListBox控件的使用
- (转载)mfc listbox 控件-ListBox控件的使用
- SilverLight技术的Listbox控件的使用
- WPF中ListBox控件的使用
- MFC中ListBox控件的使用
- MFC中Listbox控件的简单使用
- MFC中Listbox控件的简单使用
- MFC中ListBox控件的使用
- ListBox,CheckedListBox,ComboBox控件的使用
- Windows SDK下ListBox控件的使用
- MFC中Listbox控件的使用
- 使用listbox的内嵌控件
- MFC中Listbox控件的简单使用
- 调用另一个构造方法
- Label控件的使用
- 四个参数的查询案例
- AJAX的七宗罪
- 经典之贴【转】
- ListBox控件的使用
- 救人无数的七个心理寓言
- Excel VBA 语句集(1)
- jsp的实训=玩游戏(推魔兽)
- Excel VBA 语句集(2)
- 求助:new string(s,0,top+1) 谁帮忙解释一下 谢谢拉
- GCC的参数优化与环境变量
- 从今天开始,要写我的IT博客了,记录我所学到的知识;第一篇,记录关于OSI模型的描述
- 我是如何写网站的 —— UI层的下一层