求助:问一个很菜的问题!(困了我一下午了):ListItem.add()和List.remove()

hslcomputer 2004-08-11 03:38:24
我的程序很简单:
在页面放置三个ListBox框,ListBox1的Items中设置了项.
在 Page_Load 中:
ListBOx2.DataSource=ListBOx1.items;
ListBOx2.DataBind();
页面还有两个按扭:是用来将ListBox2中的项移入和移回ListBOx3中:
Button1.Click()中代码:
ListBox3.SelectIndex=-1;
ListBox3.Add(ListBox2.SelectedItem);
ListBox2.Remove(ListBox2.SelectedItem);

运行错误如下:

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 246: {
行 247: listBox1.SelectedIndex=-1;
行 248: listBox1.Items.Add(lstZiduan.SelectedItem);
行 249: lstZiduan.Items.Remove(lstZiduan.SelectedItem);
行 250: }

...全文
185 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗石绿 2004-08-11
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<script runat="server" language="C#">

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

// 给 ListBox2 和 ListBox3 赋值

if(!IsPostBack)
{
for(int i=0;i<4;i++)
{
ListBox2.Items.Add("ListBox2_"+i.ToString());
ListBox3.Items.Add("ListBox3_"+i.ToString());
}
}

this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);

}

// ListBox2 >> ListBox3
private void Button1_Click(object sender, System.EventArgs e)
{
if( ListBox2.SelectedItem != null)
{
ListItem tempItem = ListBox2.SelectedItem;
ListBox2.Items.Remove(tempItem);
ListBox3.Items.Add(tempItem);
ListBox3.SelectedIndex = -1;
}
}

// ListBox3 >> ListBox2
private void Button2_Click(object sender, System.EventArgs e)
{
if( ListBox3.SelectedItem != null)
{
ListItem tempItem = ListBox3.SelectedItem;
ListBox3.Items.Remove(tempItem);
ListBox2.Items.Add(tempItem);
ListBox2.SelectedIndex = -1;
}
}


</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox2" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 200px" runat="server"
Width="96px" Height="128px"></asp:ListBox>
<asp:ListBox id="ListBox3" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 200px"
runat="server" Width="96px" Height="120px"></asp:ListBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 104px; POSITION: absolute; TOP: 152px" runat="server"
Text=" >> "></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 152px" runat="server"
Text=" << "></asp:Button>
</form>
</body>
</HTML>
hslcomputer 2004-08-11
  • 打赏
  • 举报
回复
to alliat:
可是在页面我看到有数据啊在(ListBOx1中)
alliat 2004-08-11
  • 打赏
  • 举报
回复
我认为是这样的,你的数据是在页面LOAD时,绑定到LIST上去的,实际上LIST的ITEM为空,所以你去操作时会出错,正确的方法是,这种REMOVE和ADD就在前台用JAVASCRIPT完成,不必到后台去
暗石绿 2004-08-11
  • 打赏
  • 举报
回复
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

行 248: listBox1.Items.Add(lstZiduan.SelectedItem);


可以看出
lstZiduan.SelectedItem 是个空对象,即你的 ListBox 没有选中的项。

siugwan 2004-08-11
  • 打赏
  • 举报
回复
ListBox2.Remove(ListBox2.SelectedItem);
后加
ListBox2.SelectedIndex = -1;
试试
会不会是你的ListBox2.Remove后就没有Item了?
hslcomputer 2004-08-11
  • 打赏
  • 举报
回复
不是这个错误,这个错误编译时能发现,我这里写的不规则.
wxlada 2004-08-11
  • 打赏
  • 举报
回复
ListBOx2 ListBox2我总觉得你这些大小写中有问题。。
ASP.NET实现Gridview隐藏/显示列源码 介绍: 这篇文章演示如果让用户有显示/隐藏他们需要的GridView的列的功能,这是非常有用的,因为在GridView的所有列并不是每个的用户都需要的.用户想根据自己的需求看到想要的列.而不是显示一个巨大的gridview,霸占了整个屏幕,而是一个整洁的Gridview,而且它有所有你需要的列.对于页面的打印这也是一个非常有用的技术,因为用户可以灵活地选择GridView的列打印。 背景: RowCreated 和ItemDataBound 事件允许你用多种方式注入HTML, CSS,和JavaScript 来增强GridView 控件的功能。 文章将会演示两种显示和隐藏GridView列的方法,一种是客户端的方法,另外一种是服务段的方法. 在客户段显示和隐藏GridView的列 大部分代码是在GridView的RowCreated事件生成客户端的功能的。当GridView的Header行被创建后,一个带负号的HyperLink被插入每个Header行的单元格中用来隐藏列。 这个hyperlink通过它的onclick事件调用一个HideCol的Javascript方法,CSS类用来增加负号的大小,当每个数据行被创建的时候,一个Id将会被添加到每行中用来让Javascript区分每一行. 代码 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { GridView gridView = (GridView)sender; StringBuilder sb = new StringBuilder(); // For the header row add a link to each header // cell which can call the HideCol javascript method if (e.Row.RowType == DataControlRowType.Header) { // Loop through each cell of the row for (int columnIndex = 0; columnIndex 0) { Label columnTextLabel = new Label(); columnTextLabel.Text = e.Row.Cells[columnIndex].Text; e.Row.Cells[columnIndex].Controls.Add(columnTextLabel); } } } // Give each row an id if (e.Row.RowType == DataControlRowType.Pager) e.Row.Attributes.Add("id", gridView.ClientID + "_pager"); else e.Row.Attributes.Add("id", gridView.ClientID + "_r" + e.Row.RowIndex.ToString()); } SetupShowHideColumns方法中生成“Show Columns”下拉菜单的HTML,输出在Literal控件上面 。 代码 private void SetupShowHideColumns(GridView gridView, Literal showHideColumnsLiteral) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append(""); sb.Append("- Show Column -"); showHideColumnsLiteral.Text = sb.ToString(); } 在数据绑定到GridView之后,其余的工作由ShowHideColumns.js中的javascript来完成.当列头的hyperlink被点击的时候后,它将会传递GridView的名字,列的索引和列名给HideCol方法,这个方法能找到这一列的每个单元格,每个单元格的将添加display:none样式,用来隐藏这一列. 当选择"Show Column"中的选项后,Javascript方法ShowCol将会被调用,它将移除每个单元格的display:none样式,这一列将会被再次显示. 在服务端显示/隐藏GridView的列 服务端的例子将通过RowCreated事件给每个列头添加一个负号,这次是使用LinkButton控件.设置CommandName和CommandArgument属性,这样当通过LinkButton引发RowCommand事件时,相关的列都可以隐藏。以前隐藏的列索引存储在一个List中,这些列在建立时,将会被隐藏的。 代码 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { // For the header row add a link button to each header // cell which can execute a row command if (e.Row.RowType == DataControlRowType.Header) { // Loop through each cell of the header row for (int columnIndex = 0; columnIndex 0) { Label columnTextLabel = new Label(); columnTextLabel.Text = e.Row.Cells[columnIndex].Text; e.Row.Cells[columnIndex].Controls.Add(columnTextLabel); } } } // Hide the column indexes which have been stored in hiddenColumnIndexes foreach(int columnIndex in hiddenColumnIndexes) if (columnIndex 0) { this.GridView1ShowHideColumns.Visible = true; this.GridView1ShowHideColumns.Items.Add(new ListItem("-Show Column-", "-1")); foreach (int i in hiddenColumnIndexes) this.GridView1ShowHideColumns.Items.Add( new ListItem(columnNames[i], i.ToString())); } else { this.GridView1ShowHideColumns.Visible = false; } } 示例项目的例子: 客户端的例子: •C#.NET - Client-side example accessing data stored in session. •C#.NET - Client-side example which includes: MasterPage, UpdatePanel, GridView editing, paging and sorting, accessing data via the SqlDataSource control. •VB.NET - Client-side example accessing data stored in session. 服务端的例子 •C#.NET - Server-side example accessing data stored in session. •C#.NET - Server-side example which includes: MasterPage, UpdatePanel, GridView editing, paging and sorting, accessing data via the SqlDataSource control. •VB.NET - Server-side example accessing data stored in session. 结论: 如果你想让你的用户能够显示和隐藏在ASP.NET GridView的列,那么这种技术可能是非常有用的。 原文:http://www.codeproject.com/KB/webforms/ShowHideGridviewColumns.aspx 作者:朱祁林 出处:http://zhuqil.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
shop >前言:基于ssm分布式开发实现的电商项目(聚合工程) 注:本项目为开源项目,不能用于商业应用,仅供学习。 ### 使用工具: maven(构建项目),svn(版本控制工具),myeclipse(集成开发环境),nginx(反向代理), FastDFS (图片服务器),tomcat(web服务器),zookeeper(集群管理),mysql(数据库) Junit(测试) ### 技术栈: spring,springmvc,mybatis(框架) solr(搜索服务),redis(缓存),easyUI(后台系统页面) ### 数据库设计 tb_user用户表(id,username,password,phone,email,created,updated) tb_item商品表(id,title,sell_point,price,num,barcode,image,cid,status,created,updated) tb_cat商品分类表(id,parent_id,name,status,sort_order,is_parent,created,updated) tb_item_desc商品描述表(item_id,item_desc,created,updated) tb_item_param商品规格参数表(id,item_cat_id,param_data,created,updated) tb_item_param商品规格参数模板表(id,item_id,param_data,created,updated) tb_order订单表(payment,payment_type,post_fee,status,create_time,update_time,payment_time,consign_time,end_time,close_time,shipping_name,shipping_code,user_id,buyer_message,buyer_nick,buyer_rate) tb_order订单商品表(id,item_id,order_id,num,title,price,total_fee,pic_path) tb_order_shipping订单物流表(order_id,receiver_name,receiver_phone,receiver_mobile,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,created,updated) tb_content_category商品目录分类表(id,parent_id,name,status,sort_order,is_parent,created,updated) tb_content商品目录表(id,category_id,title,sub_title,title_desc,url,pic,pic2,content,created,updated) ## 分布式系统 ### 商品后台管理系统 ### shop-manager(管理后台) 商品的添加功能: 1.商品类目选择-easyui异步tree控件的使用 2.图片上传(fastdfs+nginx) 3.富文本编辑器使用KindEditor 4.分页使用PageHelper插件,插件是基于mybatis的拦截器接口实现的 商品的展示功能: 1.分页插件的使用PageHelper。 2.easyUIDataGrid的使用 ### 前台系统 ### shop-rest(发布服务) ### shop-search(搜索服务) * 使用solr实现搜索,内容列表使用redis缓存,使用zookeeper管理集群 ### shop-sso (单点登录系统) SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中, 用户只需要登录一次就可以访所有相互信任的应用系统。它包括 可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制。 它是目前比较流行的企业业务整合的解决方案之一。 用户登录: 1、接收用户名和密码 2、校验用户名密码 3、生成token,可以使用UUID 4、把用户信息写入redis,key就是token 5、把token写入cookie。 6、返回登录成功需要把token返回给客户端。 Session共享的: 1、tomcat做集群配置session复制。如果集群中节点很多,会形成网络风暴。推荐节点数量不要超过5个。 2、分布式架构。拆

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧