Solved

Detecting if ListViewItem or ListViewDataItem

Posted on 2011-09-15
5
1,054 Views
Last Modified: 2012-05-12
Hi

I have a Web User Control with a DropDownList and i have a parent page with a ListView which contains the Web User Control in the InsertItem and in the EditItem

And when i use the SelectedIndexChanged and use the sender object to tell where the Control Was sent from

DropDownList DropDownList1 = (DropDownList)sender;

I then assign it to the LIistViewDataItem

        ListViewDataItem ListView1 = (ListViewDataItem)DropDownList1.Parent.Parent;

If the DropDownList is being executed in the InsertItem of the ListView then it is coming through as ListViewItem but if it is being Executed in the EditItem It is coming thorgh as a ListViewDataItem

So this is where i'm getting an error and i need a way to test which one is coming through.

Sort of Like

If (lvAssets.Item == ListViewDataItem)
 Do Something;
else if (lvAssets.Item == ListViewItem)
 Do Something;

Is this Possible?


Thanks
John

0
Comment
Question by:johngrant_ks
5 Comments
 
LVL 7

Expert Comment

by:Slimfinger
ID: 36547151
Not sure if I understand your question, but maybe this is what you want

if (lvAssets.Item.GetType() == typeof(ListViewDataItem))
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36547233
Yes. You can verify the type of it using is operator.

If (lvAssets.Item is ListViewDataItem)
 Do Something;
else if (lvAssets.Item is ListViewItem)
 Do Something;
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36547396
OR
if (lvAssets.Item.ItemType == ListViewItemType.DataItem)
//your code
else if (lvAssets.Item.ItemType == ListViewItemType.ViewItem)
// your code
0
 
LVL 4

Accepted Solution

by:
guramrit earned 500 total points
ID: 36547535
ListViewDataItem is inheritted from ListViewItem. You can code as follows:

 
DropDownList DropDownList1 = (DropDownList)sender;
ListViewItem lvi = (ListViewItem)DropDownList1.Parent.Parent;

if (lvi is ListViewDataItem)
{
   // Do Something
}
else 
{
   // Do Something
}

Open in new window

0
 

Author Closing Comment

by:johngrant_ks
ID: 36557653
Hi

USing your suggestion ii found i didnt need to detect if it was a dataitem or just an item

I could just do

 DropDownList DropDownList1 = (DropDownList)sender;
        ListViewItem ListViewItem1 = (ListViewItem)DropDownList1 .Parent.Parent;

And it worked

Thank you all for your help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question