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

Find and select an item in Listview with paging

4 Answers 512 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 07 Oct 2011, 04:24 PM
Hello,

I hope someone can help me out with the following problem.

I have a usercontrol with a ListView which I have bound to a list of productimages. There are 12 items in the listview and I have set the pagesize to 4. This means I have 4 pageindexes.
In another usercontrol I fire an event with should look up the productimage in the above listview. What I want is to lookup the item in the listview which matches the parameter I send from the second usercontrol. As long as the parameter matches an productimage which is present in the selected pageindex it works all fine. But how can I look into all the items that are bound to the Listview and then select that item and set the correct pageindex

For a demo you can look at http://roosjen.uitverkoopinnederland.nl/index.aspx?contentid=233
On the left hand you can click on [Bekijk productinfo] and on the right topside the corresponding productimage should be set.

Thanks in advance.

Kind regards Patrick

4 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 12 Oct 2011, 10:05 AM
Hello Patrick,

There are two approaches you can use:
    - Look for the item on the current page, if the item is there, select it and finish. If the item is not on the current page, change the listview current page index, rebind the listview and repeat the above procedure.
    - Look for the item on the current page, if the item is there, select it and finish. If the item is not on the current page, find the record directly in the data source to which the listview is bound and calculate on which page it would be, change the listview current page index, rebind the listview and select the desired item.

Greetings,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Patrick
Top achievements
Rank 1
answered on 12 Oct 2011, 01:49 PM
Hello Iana,

Thx for your answer but I can't get it right. I have the code below implemented but I encounter the follow problems

I have 5 items bound to my ListView with a PageSize = 4, so two pages in total.

This is the scenario:

The ListView is set on PageIndex = 0, showing 4 items.
I start looking for an item with productId = 3. The first loop (CurrentPageIndex = 0) the this.lstProducts.Items.Count indicates that it holds 4 items. In the second loop the CurrentPageIndex is set to 1. Now, the this.lstProducts.Items.Count indicates that it holds 1 item.
Because the productid was found among the items of PageIndex = 0, the ListView is set to PageIndex = 0. This all correct.

But, now I have found an item with a productId that is located in de itemset in PageIndex = 1. The CurrentPageIndex  is set to 1 (one image is shown in the ListView. This is still correct.

When I start looking for another item (and CurrentPageIndex is still 0 which equals 1 item) this is what happens:

In code the CurrentPageIndex is set to 0 but now this.lstProducts.Items.Count = 1 instead of 4. In the next loop (CurrentPageIndex is set to 1) the this.lstProducts.Items.Count is also 1 (which is correct for PageIndex =1)

What I'm I doing wrong? or overlook?

Than a another question about paging of the ListView. Which event is fired when I change PageIndex. For now I bind the ListView on my Page_PreRender event but I think that is going to give problems when I try to set a specified item in the ListView (see original problem).

I hope you can help me a little bit further because it gives me a little bit of a headache by now ;-(

Thanks

int pageCount = lstProducts.PageCount;
        bool stop = false;
        for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
        {
            lstProducts.CurrentPageIndex = pageIndex;
            lstProducts.DataBind();

            foreach (Telerik.Web.UI.RadListViewDataItem itemRow in this.lstProducts.Items)
            {
                ImageButton oButton = itemRow.FindControl("SushiImage") as ImageButton;

                if (oButton != null)
                {
                    if (oButton.CommandArgument == ProductItemId.ToString())
                    {
                        stop = true;
                        currentPageIndex = pageIndex;
                        break;
                    }
                }
            }

            if (stop)
            {
                break;
            }
        }

        lstProducts.Rebind();
0
Iana Tsolova
Telerik team
answered on 14 Oct 2011, 02:45 PM
Hello Patrick,

This seems to be an issue with the databinding. I suggest that you bind the grid through the NeedDataSource. Then after changing the CurrentPageIndex, call Rebind() for the listview and proceed with the foreach Items loop. The Rebind() call at the end of the posted snippet is not neccessary.
Additionally, when changing the page index by setting the CurrentPageIndex property, no event is fired. To fire the PageIndexChaged event, you need to fire the Page command. However, I do not think this is necessary for this case as well.

All the best,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Vasssek
Top achievements
Rank 1
answered on 24 Aug 2018, 12:05 PM

Hello,

for those who need to select rad list item with paging here is my solution:

protected void RadListView1_PreRender(object sender, EventArgs e)
    {
        if (Session["ID_zoznam_MO_poziadaviek"].ToString() != "0")
        {
            int currentPageIndex = RadListView1.CurrentPageIndex;
 
            // Traverse to all pages in the RadListView until the newly inserted item is found
            for (int i = 0; i < RadListView1.PageCount; i++)
            {
                foreach (RadListViewDataItem item in RadListView1.Items)
                {
                    if (Session["ID_zoznam_MO_poziadaviek"].ToString() == item.GetDataKeyValue("ID").ToString())
                    {
                        // Select the last inserted row
                        item.FireCommandEvent(RadListView.SelectCommandName, string.Empty);
 
                        currentPageIndex = -1; // flag exit
                        Session["ID_zoznam_MO_poziadaviek"] = "0";
                        break;
                    }
                }
 
                // If item is found then exit RadListView page loop
                if (currentPageIndex.Equals(-1))
                    break;
 
                // Go to next RadListView page
                currentPageIndex++;
                if (currentPageIndex >= RadListView1.PageCount)
                    currentPageIndex = 0;
                RadListView1.CurrentPageIndex = currentPageIndex;
 
                RadListView1.Rebind();
            }
        }
    }

 

In the RadListView1_PreRender method I compare row ID with value saved into session variable Session["ID_zoznam_MO_poziadaviek"] set on different page... ID is declared at aspx RadListView definition as DataKeyNames="ID"

Regards

Vasssek

 

 

 

Tags
ListView
Asked by
Patrick
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Patrick
Top achievements
Rank 1
Vasssek
Top achievements
Rank 1
Share this question
or