C# web中listbox的使用以及删除等操作
2013-08-09 16:59
411 查看
今天研究了listbox的用法,其中包括listbox项的添加 以及批量删除。在这里记录下来希望大家能够少走弯路
<body>
<form id="form1" runat="server">
<div class="main">
<div class="leftcol">
<div style="text-align:right; height:500px">
<asp:ListBox ID="ListBox1" runat="server" Width="200px" Height="500px" SelectionMode="Multiple"></asp:ListBox>
</div>
</div>
<div class="midcol" id="parent">
<table width="80%" id="child">
<tr>
<td align="center">
<asp:Button ID="Button2" runat="server" Text="部分添加" onclick="Button2_Click"
/>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
</td>
</tr>
</table>
</div>
<div class="rightcol">
<asp:ListBox ID="ListBox2" runat="server" Height="500px" Width="200px" SelectionMode="Multiple"></asp:ListBox>
</div>
</div>
</form>
</body>
//部分添加代码:
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex < 0)
{
ClientScript.RegisterStartupScript(GetType(), "", "alert('请选择添加的项')", true);
return;
}
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (ListBox2.Items.Count > 0)
{
if (ListBox2.Items.Contains(ListBox1.Items[i]))
{
ClientScript.RegisterStartupScript(GetType(), "", "alert('添加的项已经重复')", true);
break;
}
else
{
ListBox2.Items.Add(ListBox1.Items[i]);
}
}
else
{
this.ListBox2.Items.Add(ListBox1.Items[i]);
}
}
}
}
批量删除选择项
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
ListBox2.Items.Remove(ListBox2.Items[i]);
i = i - 1; 这里i一定要减1,因为集合中已经移除了一项,相应的i也应该减1。
}
}
}
<body>
<form id="form1" runat="server">
<div class="main">
<div class="leftcol">
<div style="text-align:right; height:500px">
<asp:ListBox ID="ListBox1" runat="server" Width="200px" Height="500px" SelectionMode="Multiple"></asp:ListBox>
</div>
</div>
<div class="midcol" id="parent">
<table width="80%" id="child">
<tr>
<td align="center">
<asp:Button ID="Button2" runat="server" Text="部分添加" onclick="Button2_Click"
/>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
</td>
</tr>
</table>
</div>
<div class="rightcol">
<asp:ListBox ID="ListBox2" runat="server" Height="500px" Width="200px" SelectionMode="Multiple"></asp:ListBox>
</div>
</div>
</form>
</body>
//部分添加代码:
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex < 0)
{
ClientScript.RegisterStartupScript(GetType(), "", "alert('请选择添加的项')", true);
return;
}
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (ListBox2.Items.Count > 0)
{
if (ListBox2.Items.Contains(ListBox1.Items[i]))
{
ClientScript.RegisterStartupScript(GetType(), "", "alert('添加的项已经重复')", true);
break;
}
else
{
ListBox2.Items.Add(ListBox1.Items[i]);
}
}
else
{
this.ListBox2.Items.Add(ListBox1.Items[i]);
}
}
}
}
批量删除选择项
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
ListBox2.Items.Remove(ListBox2.Items[i]);
i = i - 1; 这里i一定要减1,因为集合中已经移除了一项,相应的i也应该减1。
}
}
}
相关文章推荐
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- C#使用DotNetZip对zip压缩包进行添加删除操作
- HttpClient, 使用C#操作Web
- HttpClient, 使用C#操作Web
- C#操作Excel的类以及其使用举例说明
- HttpClient, 使用C#操作Web
- C# HttpClient, 使用C#操作Web
- C#使用tabcontrol控件可操作多窗体嵌入以及双击窗口名称可关闭
- HttpClient, 使用C#操作Web
- 实验:模拟场景中误删除mysql数据库表,然后使用全备份以及二进制日志文件恢复操作
- HttpClient, 使用C#操作Web
- C#中Cookies的存取删除,前台使用jquery.cookie操作cookie
- SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (二)
- C#中Cookies的存取删除,前台使用jquery.cookie操作cookie
- HttpClient, 使用C#操作Web
- 使用C#对Excel进行读写操作(NPOI)以及使用EF对Mysql进行CURD
- vector中 迭代器的使用 以及元素的去重、排序、插入、删除操作。 【小笔记】
- SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)
- HttpClient, 使用C#操作Web