This is a migrated thread and some comments may be shown as answers.

Where is the item.itemindex property?

7 Answers 377 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 12 Feb 2010, 02:55 PM
Hi, I am trying to use a RadListView in the same way I use a RadGrid in that I am trying to get the datakeyvalue for a specific dataitem when in the ItemDataBound method.

In a RadGrid, I can do the following:

e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][myDataKey] 

to get the value of each datakey and then set client-side onclick attributes for a hyperlink so that clicking the link opens a RadWindow, passing in the row ID.

However, e.Item for a RadListView does not have an ItemIndex property, so how can I do the same for a RadListView?
ie,

e.Item.OwnerListView.DataKeyValues[???][myDataKey]

Regards,
Jonathan

7 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 16 Feb 2010, 09:39 AM
Hello Jonathan,

RadListView exposes DisplayIndex property that specifies the order of the item when displayed. Hence you can pass that property value in order to extract the key values for a given RadListViewDataItem, for example:

C#
string keyVal = e.Item.OwnerListView.DataKeyValues[e.Item.DisplayIndex]["CustomerID"].ToString();

VB.NET
Dim keyVal As String = e.Item.OwnerListView.DataKeyValues(e.Item.DisplayIndex)("CustomerID").ToString()


Kind regards,
Sebastian
the Telerik team


Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jonathan
Top achievements
Rank 1
answered on 16 Feb 2010, 09:48 AM
Thanks Sebastian,

I missed that property in the list.

Regards,
Jonathan
0
Jonathan
Top achievements
Rank 1
answered on 16 Feb 2010, 10:43 AM
Sebastian,

The property e.Item.DisplayIndex only seems to be available during RadListView1_ItemInserted and RadListView1_ItemUpdated but NOT during RadListView1_ItemDataBound.

This means I still cannot add the attribute I need, ie

 

(e.Item.FindControl("lnkbEdit"as LinkButton).Attributes.Add("onclick",   
"OpenwndRM('" + e.Item.OwnerListView.DataKeyValues[ItemIndex]["ResearchMethodID"].ToString() + "');return false;"); 

This sets the client-side click event to open a RadWindow, passing in the row identifier to the javascript function.

Regards,
Jonathan
0
Sebastian
Telerik team
answered on 16 Feb 2010, 11:20 AM
Hello Jonathan,

Can you please confirm that you tried this code inside the ItemDataBound handler:

Attributes.Add("onclick",   
"OpenwndRM('" + e.Item.OwnerListView.DataKeyValues[e.Item.DisplayIndex]["ResearchMethodID"].ToString() + "');return false;");

Also note that the more appropriate place to set the attribute for the link button is the ItemCreated event of RadListView (see this topic for details).

Best regards,
Sebastian
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jonathan
Top achievements
Rank 1
answered on 16 Feb 2010, 11:26 AM
Hi Sebastian, here is the code lifted from the code-behind:

 

protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)

 

{

(e.Item.FindControl(

"lnkbEdit") as LinkButton).Attributes.Add("onclick", "OpenwndRM('" + e.Item.OwnerListView.DataKeyValues[e.Item.displayitem]["ResearchMethodID"].ToString() + "');return false;");

 

}

The displayitem of e.Item.displayItem is flagged as an unknown member.

Regards,
Jonathan

PS using dll version 2009.3.1314.35

0
Sebastian
Telerik team
answered on 16 Feb 2010, 11:30 AM
Hello Jonathan,

The property I am referring to is DisplayIndex, not displayitem. Review the code snippets from my previous posts and let me know if I am leaving something obvious out.

Regards,
Sebastian
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jonathan
Top achievements
Rank 1
answered on 16 Feb 2010, 11:51 AM
Hi Sebastian,
Sorry, my mistake - a simple typo.

However, I have managed to get it working  by casting e.item as a RadListViewDataItem.

So this now works:

 

protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)

 

{

 

RadListViewDataItem item = (RadListViewDataItem)e.Item;

 

(e.Item.FindControl(

"lnkbEdit") as LinkButton).Attributes.Add("onclick", "OpenwndRM('" + e.Item.OwnerListView.DataKeyValues[item.DisplayIndex]["ResearchMethodID"].ToString() + "');return false;");

 

}


Thanks for your help.
Regards,
Jonathan

Tags
ListView
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Jonathan
Top achievements
Rank 1
Share this question
or