ASP.NET listBbox控件用法
ListBox基本功能使用方法
2011-06-09 13:23:16| 分类: .NET/C# | 标签:listbox基本功能使用方法 |举报 |字号大中小 订阅
- ListBox基本功能使用方法
ListBox基本功能首先是列表项的添加,客户端实现代码添加在listbox实例化代码中间,例如:
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>
若在服务器端实现,为避免每次加载时执行添加列表项,上述代码包含在下面代码中:
if(!IsPostBack)
{
}
WebForm页面上须添加2个listbox(listbox1和lixtbox2)和2个命令按钮,listbox1不为空。列表项从listbox1添加到listbox2须在Button1单击事件中调用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);
若要从listbox2中删除列表项的话须在Button2单击事件中调用Remove方法:
ListBox2.Items.Remove(ListBox2.SelectedValue);
列表项从listbox1添加到listbox2后,列表项从listbox1中删除:
int i=0;
while(i<ListBox1.Items.Count)
{
if(ListBox1.Items[i].Selected==true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i+=1;
}
这样只能实现单项添加,想要实现多项添加,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允许多项选中。
在Button1单击事件中添加
foreach(ListItem MyItem in ListBox1.Items)
if(MyItem.Selected==true)
ListBox2.Items.Add(MyItem);
想要一次清空ListBox2中所有选项可在Button2单击事件中调用clear方法,
ListBox2.Items.Clear();
若列表项已经添加,不允许二次添加,Button1单击事件中的代码包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}
ListBox与数据库绑定就是指定他的DataSource和DataTextField属性,
ListBox2.DataSource=数据源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();
<script type="text/javascript">
function SelectAll()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var length = lst1.options.length;
var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
for(var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
string.value+=v;
}
}
function DelAll()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var length = lst2.options.length;
for(var i=length;i>0;i--)
{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
}
function SelectOne()
{
var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst1.selectedIndex;
var length = lst2.options.length;
var isExists = false;
if(lstindex<0)
return;
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
string.value+=v;
}
else
{
alert("所选条目已经存在");
return false;
}
}
function DelOne()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
{
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
}
}
</script>
需要解释的是由于JS脚本是在客户端执行的,因此服务器端控件是无法调用JS的,由于ID无法被找到,但用<%=lb_NewName.ClientID %>的方法就巧妙的解决得该问题,是asp控件拥有客户端id,这样就可以调用了。
希望对大家有所帮助!
ASP.NET中添加控件ListBox , 属性设为 Multiple , 则可进行多选.
就以两个listbox之间多选添加项目为例.
两个控件为listboxleft , listboxright 定义了一个动态数组用于中间存储 arrRight .具体代码如下:
//读取右边选中项目
ArrayList arrRight = new ArrayList();
foreach(ListItem item in this.ListBoxRight.Items) //按类型listitem读取listbox中选定项
{
if(item.Selected) //判断是否选中
{
arrRight.Add(item);
}
}
//右边移除选定项目 左边添加
foreach(ListItem item in arrRight)
{
this.ListBoxLeft.Items.Add(item);
this.ListBoxRight.Items.Remove(item);
}
不能将item的添加删除直接写在if(item.Selected){}内,因为项目remove后会出现错误
添加两个listbox (ListBoxAll , ListBoxUser) 两个按钮( ButtonListDel >> , ButtonListAdd <<)
按钮的代码为:
private void ButtonListDel_Click(object sender, System.EventArgs e)
{
//listbox >> 删除listboxuser选中项目 将其添加入listboxall
if(this.ListBoxUser.SelectedIndex != -1)
{
this.ListBoxAll.Items.Add(this.ListBoxUser.SelectedItem.Value);
this.ListBoxUser.Items.Remove(this.ListBoxUser.SelectedItem.Value);
}
}
private void ButtonListAdd_Click(object sender, System.EventArgs e)
{
//listbox <<
if(this.ListBoxAll.SelectedIndex != -1)
{
this.ListBoxUser.Items.Add(this.ListBoxAll.SelectedItem.Value);
this.ListBoxAll.Items.Remove(this.ListBoxAll.SelectedItem.Value);
}
}
1 为了确保添加不会重复 填充listbox时使两边无重复项目.
完成listbox里项目的添加、删除的关键代码:
1.通过AddRange方法添加项目:this.lbyx.Items.AddRange(new object[] {"北京","上海","天津","成都","广州","深圳","武汉"});
2.添加items:this.lbbx.Items.Add(this.lbyx.Text);
3.清空列表内的所有items:this.lbbx.Items.Clear();
4.当前所选项的编号获取:this.lbbx.SelectedIndex
5.删除某项:this.lbbx.Items.RemoveAt(this.lbbx.SelectedIndex);
在从一个ListBox选择内容copy到另外一个ListBox时候用下面的方法:
if (ListBox2.Items.IndexOf(ListBox1.SelectedItem) == -1)
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedValue));
//ListBox2.Items.Add(ListBox1.SelectedItem); <--用这个会记录状态,ListBox2不支持Multiple就出错了
}
===================================================================
<asp:listbox width="100px" runat=server>
<asp:listitem>Roman</asp:listitem>
<asp:listitem>Arial Black</asp:listitem>
<asp:listitem>Garamond</asp:listitem>
<asp:listitem>Somona</asp:listitem>
<asp:listitem>Symbol</asp:listitem>
</asp:listbox>
void RemoveAllBtn_Click(Object Src, EventArgs E) {
while (InstalledFonts.Items.Count != 0) {
AvailableFonts.Items.Add(new ListItem(InstalledFonts.Items[0].Value));
InstalledFonts.Items.Remove(InstalledFonts.Items[0].Value);
}
}
当数据源改为
<asp:listbox width="100px" runat=server>
<asp:listitem value="1">Roman</asp:listitem>
<asp:listitem value="bbb">Arial Black</asp:listitem>
<asp:listitem value="333">Garamond</asp:listitem>
<asp:listitem value="4">Somona</asp:listitem>
<asp:listitem value="5">Symbol</asp:listitem>
</asp:listbox>
listbox.items.count会一直为总数,不会顺while循环的变化,可以修改为如果方法:
#region button
private void btnRemoveAll_Click(object sender, System.EventArgs e)
{
while (lstSelDpt.Items.Count != 0)
{
lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.Items.Count-1]);
lstSelDpt.Items.RemoveAt(lstSelDpt.Items.Count-1);
}
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
while(lstSelDpt.SelectedIndex != -1)
{
lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
lstSelDpt.Items.Remove(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
}
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
while (lstAllDpt.SelectedIndex != -1)
{
lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
}
}
private void btnAddAll_Click(object sender, System.EventArgs e)
{
while (lstAllDpt.Items.Count != 0)
{
lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
}
}
#endregion
用dotnet做一个项目的过程中,遇到了一个ListBox的问题:通过在一个ListBox中双击,把选中的项添加到另一个ListBox中,但ListBox控件本身并没有该事件,那么如何实现呢?我就想到了客户端脚本javascrit,通过查阅相关资料,终于把这个问题解决了,现在写出来与大家分享,希望能对大家有所帮助。
这里有三个问题:
第一:双击所要执行的javascript代码是什么?
注意:javascript代码的语法要正确,即每一行都要以“;”结尾;
function change()
{
var addOption=document.createElement("option");
var index1;
if(document.Form1.ListBox1.length==0)return(false);
index1=document.Form1.ListBox1.selectedIndex;
if(index1<0)return(false);
addOption.text=document.Form1.ListBox1.options(index1).text;
addOption.value=document.Form1.ListBox1.value;
document.Form1.ListBox2.add(addOption);
document.Form1.ListBox1.remove (index1);
}
第二:如何将 javascript 代码转换为C#代码?
public static void ListBox_DblClick(Page page,System.Web.UI.WebControls.WebControl webcontrol,string SourceControlName,string TargetControlName)
{
SourceControlName = "document.Form1." + SourceControlName;
TargetControlName = "document.Form1." + TargetControlName;
string js = "<script language=javascript> function change(SourceControlName,TargetControlName)";
js += "{";
js += "var addOption=document.createElement('option'); \n";
js += " var index1; \n";
js += "if(SourceControlName.length==0)return(false);\n";
js += " index1=SourceControlName.selectedIndex; \n ";
js += " if(index1<0)return(false);\n";
js += " addOption.text=SourceControlName.options(index1).text; \n";
js += "addOption.value=SourceControlName.value; \n";
js += "TargetControlName.add(addOption); \n";
js += "SourceControlName.remove (index1) \n";
js +="}";
js += "</script>";
//注册该 javascript ;
page.RegisterStartupScript("",js);
//为控件添加双击事件;
webcontrol.Attributes.Add("onDblClick","change(" + SourceControlName + "," + TargetControlName + ");");
}
在该方法中,SourceControlName是要绑定双击事件的控件,TargetControlName是接收双击事件选定项的控件。
这里有一个问题,如何让对象作为参数传给javascript的change函数,我这里采用的是用 SourceControlName ,TargetControlName 来传递两个ListBox的Name, 然后与“document.Form1.“组合成一个串来传递给javascript的change函数,即
SourceControlName = "document.Form1." + SourceControlName;
TargetControlName = "document.Form1." + TargetControlName;
第三:如何为控件添加双击事件?
用ControlName.Attributes.Add(“属性名称”,“函数名称或代码”);
ASP.NET listBbox控件用法的更多相关文章
- asp.net Listbox控件用法
2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...
- asp.net中Repeater控件用法笔记
大家可能都对datagrid比较熟悉,但是如果在数据量大的时候,我们就得考虑使用 repeater作为我们的数据绑定控件了.Repeater控件与DataGrid (以及DataList)控件的主要区 ...
- CustomValidator控件用法
虽然大部分时间一直从事asp.net的开发,对于一些常用的asp.net服务器端验证控件及它们的组合使用比较熟悉,如:CompareValidator ——比较验证控件RangeValidator — ...
- ASP.NET ValidationSummary 控件
ASP.NET ValidationSummary 控件 Validation 服务器控件 定义和用法 ValidationSummary 控件用于在网页.消息框或在这两者中内联显示所有验证错误的摘要 ...
- Jquery + css 日期控件用法实例.zip
/*==============================================================================** Filename:common.j ...
- asp.net分页控件
一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Reg ...
- asp.net ajax控件tab扩展,极品啊,秒杀其它插件
说明:asp.net ajax控件tab要设置width和height,而且在线文本编辑器放能够放入tab中,也必须是asp.net的控件型在线文本,例如fckeditor,下面是我设置好的配置. & ...
- javascript获取asp.net服务器端控件的值
代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...
- ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
随机推荐
- logstash 根据type 判断输出
# 更多ELK资料请访问 http://devops.taobao.com 一.配置前需要注意: 1.Use chmod to modify nginx log file privilege. E.g ...
- 【转载】详解java类的生命周期
原文地址:http://blog.csdn.net/zhengzhb/article/details/7517213 引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑, ...
- 网易云课堂_程序设计入门-C语言_第五周:函数_2完数
2 完数(5分) 题目内容: 一个正整数的因子是所有可以整除它的正整数.而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数.例如6=1+2+3(6的因子是1,2,3). 现在,你要写一个程序, ...
- java日期处理 calendar 和date
抄的人家的,仅作学习使用. java Date获取 年月日时分秒 package com.util; import java.text.DateFormat; import java.ut ...
- Maven 搭建与my-app项目测试
前提条件,安装jdk1.6及以上版本,并配置JAVA_HOME 首先,下载Maven3.2.2,附下载地址:http://mirror.bit.edu.cn/apache/maven/maven-3/ ...
- IOS app启动过程
1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 3.delegate对象开始处理(监 ...
- 20151204--JDBC
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...
- 常用DOM整理
常用DOM整理 前言: html为document搭建了一棵DOM树,这棵树就是有一系列Node节点所构成的.他为我们定义了文档的结构. Node类型: Node.ELEMENT_NODE(1); ...
- 深入A标签点击触发事件而不跳转的详解
本文介绍下,当点击A标签时,触发事件但不跳转的实现方法,有需要的朋友参考下吧. 点击页面上的空链接,点击后页面自动刷新,并会定位到页面顶端. 不过,有时需要点击#页面但不作跳转,可以这样写: < ...