Here is my scenario:
I have a ListView with a bunch of Items. The ListView is tied to a DataPager which controls the paging. Currently, I have it setup where the DataPager's PageSize is 20. The ListView is on a page called Albums.aspx. When the user clicks the View link under an album, it fires the event:
protected void lvwAlbums_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.Equals("View"))
{
AlbumId = int.Parse(e.CommandArgument.ToString());
Response.Redirect("~/Albums/Edit.aspx?AlbumId=" + AlbumId.ToString());
}
}
Once I go to the Edit.aspx page, I can view the songs on the album. On the Edit.aspx page, there is an edit link which will take me back to the Albums.aspx page and allow me to add and remove songs from the album. If the album is on the firstpage of the listview, it works fine, but if it is on the second page or anything but the first, it does not find the item and it defaults to the first item on the first page for editing. Ideally, what I would like to do is pass through the query string the current page that the item is on and set the page if the user clicks the edit link on Edit.aspx, but I am stuck here and can't figure out how to do it.
Currently, to get the DisplayIndex of the Item to edit, I am doing this, but this just seems to loop through only the items on a single page, not through every item in the ListView, so if I am on item 21, which would be page 2, it never finds it and returns 0.
private int FindEditItemIndex(string albumName)
{
foreach (ListViewDataItem item in this.lvwAlbums.Items)
{
if (item.ItemType == ListViewItemType.DataItem)
{
Literal lit = (Literal)item.FindControl("litAlbumName");
if (lit != null)
{
if (albumName == lit.Text)
{
return item.DisplayIndex;
}
}
}
}
return 0;
}