不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
ListBox使用小结
编辑:dnawo 日期:2008-05-27
还是用例子说话
,本例主要演示了下对 ListBox 项的添加、修改、删除等操作,应着重理解下单项操作和多项操作的区别,以及多项移动的一些小技巧。
Default.aspx:
Default.aspx.cs:

Default.aspx:
复制内容到剪贴板
程序代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td align="left" rowspan="5" style="width: 90px" valign="middle">
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="80px" SelectionMode="Multiple" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="111">aaa</asp:ListItem>
<asp:ListItem Value="222">bbb</asp:ListItem>
<asp:ListItem Value="333">ccc</asp:ListItem>
<asp:ListItem Value="444">ddd</asp:ListItem>
<asp:ListItem Value="555">eee</asp:ListItem>
<asp:ListItem Value="666">fff</asp:ListItem>
<asp:ListItem Value="777">ggg</asp:ListItem>
</asp:ListBox></td>
<td style="width: 63px">
<asp:Button ID="Button1" runat="server" Text="单项:移除→" OnClick="Button1_Click" /></td>
<td align="center" rowspan="5" style="width: 84px" valign="middle">
<asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="80px" SelectionMode="Multiple"></asp:ListBox></td>
</tr>
<tr>
<td style="width: 63px">
<asp:Button ID="Button2" runat="server" Text="单项:移除←" OnClick="Button2_Click" /></td>
</tr>
<tr>
<td style="width: 63px">
<asp:Button ID="Button3" runat="server" Text="多项:移除→" OnClick="Button3_Click" /></td>
</tr>
<tr>
<td style="width: 63px; height: 26px">
<asp:Button ID="Button4" runat="server" Text="多项:移除←" OnClick="Button4_Click" /></td>
</tr>
<tr>
<td style="width: 63px; height: 26px">
<asp:Button ID="Button5" runat="server" Text=" 清空列表2 " OnClick="Button5_Click" /></td>
</tr>
</table>
<asp:TextBox ID="TextBox1" runat="server" Width="20px" Visible="False"></asp:TextBox>
Text:
<asp:TextBox ID="TextBox2" runat="server" Width="60px"></asp:TextBox>
Value:
<asp:TextBox ID="TextBox3" runat="server" Width="60px"></asp:TextBox>
<asp:Button ID="Button6" runat="server" Text="修改" OnClick="Button6_Click" /><br /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td align="left" rowspan="5" style="width: 90px" valign="middle">
<asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="80px" SelectionMode="Multiple" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="111">aaa</asp:ListItem>
<asp:ListItem Value="222">bbb</asp:ListItem>
<asp:ListItem Value="333">ccc</asp:ListItem>
<asp:ListItem Value="444">ddd</asp:ListItem>
<asp:ListItem Value="555">eee</asp:ListItem>
<asp:ListItem Value="666">fff</asp:ListItem>
<asp:ListItem Value="777">ggg</asp:ListItem>
</asp:ListBox></td>
<td style="width: 63px">
<asp:Button ID="Button1" runat="server" Text="单项:移除→" OnClick="Button1_Click" /></td>
<td align="center" rowspan="5" style="width: 84px" valign="middle">
<asp:ListBox ID="ListBox2" runat="server" Height="150px" Width="80px" SelectionMode="Multiple"></asp:ListBox></td>
</tr>
<tr>
<td style="width: 63px">
<asp:Button ID="Button2" runat="server" Text="单项:移除←" OnClick="Button2_Click" /></td>
</tr>
<tr>
<td style="width: 63px">
<asp:Button ID="Button3" runat="server" Text="多项:移除→" OnClick="Button3_Click" /></td>
</tr>
<tr>
<td style="width: 63px; height: 26px">
<asp:Button ID="Button4" runat="server" Text="多项:移除←" OnClick="Button4_Click" /></td>
</tr>
<tr>
<td style="width: 63px; height: 26px">
<asp:Button ID="Button5" runat="server" Text=" 清空列表2 " OnClick="Button5_Click" /></td>
</tr>
</table>
<asp:TextBox ID="TextBox1" runat="server" Width="20px" Visible="False"></asp:TextBox>
Text:
<asp:TextBox ID="TextBox2" runat="server" Width="60px"></asp:TextBox>
Value:
<asp:TextBox ID="TextBox3" runat="server" Width="60px"></asp:TextBox>
<asp:Button ID="Button6" runat="server" Text="修改" OnClick="Button6_Click" /><br /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs:
复制内容到剪贴板
程序代码

using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox1 选定项移到 ListBox2 第一项,只支持单项移动
/// </summary>
protected void Button1_Click(object sender, EventArgs e)
{
//SelectedIndex,SelectedValue
if (ListBox1.SelectedIndex >= 0)
{
ListItem li = new ListItem(ListBox1.Items[ListBox1.SelectedIndex].Text, ListBox1.Items[ListBox1.SelectedIndex].Value);
ListBox1.Items.Remove(li);
ListBox2.Items.Insert(0, li);
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox2 选定项移到 ListBox1 最末一项,只支持单项移动
/// </summary>
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox2.SelectedIndex >= 0)
{
ListItem li = new ListItem(ListBox2.Items[ListBox2.SelectedIndex].Text, ListBox2.Items[ListBox2.SelectedIndex].Value);
ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
ListBox1.Items.Add(li);
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox1 选定项移到 ListBox2 开头,支持多项移动
/// </summary>
protected void Button3_Click(object sender, EventArgs e)
{
ListItem li;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = ListBox1.Items.Count-1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
li = new ListItem(ListBox1.Items[i].Text, ListBox1.Items[i].Value);
ListBox1.Items.Remove(li);
ListBox2.Items.Insert(0, li);
}
}
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox2 选定项移到 ListBox1 末尾,支持多项移动
/// </summary>
protected void Button4_Click(object sender, EventArgs e)
{
ListItem li;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = ListBox2.Items.Count-1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
li = new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value);
ListBox2.Items.RemoveAt(i);
ListBox1.Items.Add(li);
}
}
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 清空 ListBox2
/// </summary>
protected void Button5_Click(object sender, EventArgs e)
{
ListBox2.Items.Clear();
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 显示 ListBox1 选定项的内容,以便修改
/// </summary>
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = Convert.ToString(ListBox1.SelectedIndex);
TextBox2.Text = ListBox1.Items[ListBox1.SelectedIndex].Text;
TextBox3.Text = ListBox1.Items[ListBox1.SelectedIndex].Value;
}
/// <summary>
/// 修改 ListBox1 选定项
/// </summary>
protected void Button6_Click(object sender, EventArgs e)
{
int i = Convert.ToInt16(TextBox1.Text);
ListBox1.Items[i].Text = TextBox2.Text;
ListBox1.Items[i].Value = TextBox3.Text;
}
}
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox1 选定项移到 ListBox2 第一项,只支持单项移动
/// </summary>
protected void Button1_Click(object sender, EventArgs e)
{
//SelectedIndex,SelectedValue
if (ListBox1.SelectedIndex >= 0)
{
ListItem li = new ListItem(ListBox1.Items[ListBox1.SelectedIndex].Text, ListBox1.Items[ListBox1.SelectedIndex].Value);
ListBox1.Items.Remove(li);
ListBox2.Items.Insert(0, li);
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox2 选定项移到 ListBox1 最末一项,只支持单项移动
/// </summary>
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox2.SelectedIndex >= 0)
{
ListItem li = new ListItem(ListBox2.Items[ListBox2.SelectedIndex].Text, ListBox2.Items[ListBox2.SelectedIndex].Value);
ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
ListBox1.Items.Add(li);
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox1 选定项移到 ListBox2 开头,支持多项移动
/// </summary>
protected void Button3_Click(object sender, EventArgs e)
{
ListItem li;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = ListBox1.Items.Count-1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
li = new ListItem(ListBox1.Items[i].Text, ListBox1.Items[i].Value);
ListBox1.Items.Remove(li);
ListBox2.Items.Insert(0, li);
}
}
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 将 ListBox2 选定项移到 ListBox1 末尾,支持多项移动
/// </summary>
protected void Button4_Click(object sender, EventArgs e)
{
ListItem li;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = ListBox2.Items.Count-1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
li = new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value);
ListBox2.Items.RemoveAt(i);
ListBox1.Items.Add(li);
}
}
}
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 清空 ListBox2
/// </summary>
protected void Button5_Click(object sender, EventArgs e)
{
ListBox2.Items.Clear();
Label1.Text = "ListBox1总项数:" + ListBox1.Items.Count + ",ListBox2总项数:" + ListBox2.Items.Count;
}
/// <summary>
/// 显示 ListBox1 选定项的内容,以便修改
/// </summary>
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = Convert.ToString(ListBox1.SelectedIndex);
TextBox2.Text = ListBox1.Items[ListBox1.SelectedIndex].Text;
TextBox3.Text = ListBox1.Items[ListBox1.SelectedIndex].Value;
}
/// <summary>
/// 修改 ListBox1 选定项
/// </summary>
protected void Button6_Click(object sender, EventArgs e)
{
int i = Convert.ToInt16(TextBox1.Text);
ListBox1.Items[i].Text = TextBox2.Text;
ListBox1.Items[i].Value = TextBox3.Text;
}
}
评论: 0 | 引用: 0 | 查看次数: 4135
发表评论
请登录后再发表评论!