The new ListView control in ASP.Net is pretty cool. It is really flexible and often makes life pretty easy for me. But there is one thing that doesn't make sense to me.
It isn't simple to figure out which item is the "Edit Item" during databinding. It isn't impossible, but it seems more complicated then I'd expect. During ItemDataBound() you can test the item's type:
if (e.Item.ItemType == ListViewItemType.DataItem) { //do something }
Remember that with a ListView, there are no rows, so we refer to Items.
Unfortunately, the options for ListViewItemType only include DataItem, InsertItem, and EmptyItem. All of those are helpful, but what about EditItem? So the next logical thing to do is use the ListView's EditIndex. All I'd have to do is compare the current index to the EditIndex and I'll know if I'm on the edit item. ListViewItemEventArgs has an Item property but there doesn't seem to be an Index or CurrentIndex property. So the comparison won't be so easy. We'll here it is, the way to figure it out:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { ListViewDataItem dataItem = (ListViewDataItem)e.Item; if (dataItem.DisplayIndex == ListView1.EditIndex) { //do something } } }
I hope this information is helpful.
Remember Me
.Net (36) asp.net (7) Ben (12) Blog (6) Code Camp (25) Entertainment (2) Family (14) Fun Stuff (4) General (17) Hiking (1) Microsoft (9) Movies (2) NoDeNUG.org (3) Orbius (2) Philly.Net (45) SQL Server (3) Tech-Ed 2007 (3) Technology (21) Travel (1) Vista (7) Web (8)
Powered by: newtelligence dasBlog 1.9.6264.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2008, Andrew Schwam
E-mail