Find and select an item in Listview with paging

4 posts, 0 answers
  1. Patrick
    Patrick avatar
    20 posts
    Member since:
    Feb 2008

    Posted 07 Oct 2011 Link to this post

    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
  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 12 Oct 2011 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Patrick
    Patrick avatar
    20 posts
    Member since:
    Feb 2008

    Posted 12 Oct 2011 Link to this post

    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();
  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 14 Oct 2011 Link to this post

    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
Back to Top