在ItemEditing事件中使用EditItem属性经常为null,
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.InsertItemPosition = InsertItemPosition.None;
ListView1.EditIndex = e.NewEditIndex;//e.NewEditIndex为0时,ListView1.EditItem总会为null
if (ListView1.EditItem != null)
{
//
}
}
{
ListView1.InsertItemPosition = InsertItemPosition.None;
ListView1.EditIndex = e.NewEditIndex;//e.NewEditIndex为0时,ListView1.EditItem总会为null
if (ListView1.EditItem != null)
{
//
}
}
调试了半天才发现,只要当EditIndex=0时(即编辑第一条记录)EditItem属性总是为空
用Reflector反射ListView的EditItem查到原因代码如下:(this._editIndex > 0应该改为this._editIndex >= 0)
public virtual ListViewItem EditItem
{
get
{
if ((this._editIndex > 0) && (this._editIndex < this.Items.Count))
{
return this.Items[this._editIndex];
}
return null;
}
}
后来Google了一下,发现园子里早有朋友反映了这个BUG (第一次用ListView,就抓到BUG),可MS到目前仍未修复。。。{
get
{
if ((this._editIndex > 0) && (this._editIndex < this.Items.Count))
{
return this.Items[this._editIndex];
}
return null;
}
}
临时的解决方法:用ListView1.Items[ListView1.EditIndex]代替ListView1.EditItem