ListView with DataPager not working

后端 未结 7 921
[愿得一人]
[愿得一人] 2021-02-07 05:00

From everything I\'ve read, it seemed that adding paging to a ListView control should be dead simple, but it\'s not working for me. After adding the ListView and DataPager contr

相关标签:
7条回答
  • 2021-02-07 05:29

    We need to databind list view again in OnPreRender event.

    protected override void OnPreRender(EventArgs e)
            {
                ListView1.DataBind();
                base.OnPreRender(e);
            }
    

    --Update

    After working on a few list views with asp.net ajax, I saw a solution that makes more sense than the above one. You would normally data bind Listview on page load method or a button click event handler and when there is post back the data binding would be lost as described above in the problem. So, we need to data bind again on page properties changed event handler for the list view.

    ListView_PagePropertiesChanged(object sender, EventArgs e)
    {
    ListView.DataSource=someDatasource;
    ListView.DataBind()
    }
    
    0 讨论(0)
  • 2021-02-07 05:29
    <asp:ListView ID="ListView1" runat="server" DataSourceID="sdsImages">
        <ItemTemplate>
            <div class="photo sample12">
                    <asp:Image ID="img_Galerie" runat="server" ImageUrl='<%# "~/imageHandler.ashx?ID=" + Eval("ImageID") %>' />
            </div>
        </ItemTemplate>
    </asp:ListView>
    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" QueryStringField="ImageID">
        <Fields>
            <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
            <asp:NumericPagerField />
            <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
        </Fields>
    </asp:DataPager>
    <asp:SqlDataSource ID="sdsImages" runat="server"
        ConnectionString="<%$ ConnectionStrings:DBCS %>"
        SelectCommand="SELECT ImageID FROM  Images ">
    

    0 讨论(0)
  • 2021-02-07 05:36

    Bind the listview at datapager's pre render event not at page load. Please see the solution here

    0 讨论(0)
  • 2021-02-07 05:36

    try this:

    protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        ListView1.DataSource = productList;
        ListView1.DataBind();
        DataPager1.DataBind();
    }
    
    0 讨论(0)
  • 2021-02-07 05:38

    One More Solution, Its Simple, Just Get "ID" in "QUERY-STRING" from the Database, Now Set it to the Pager Control Property as [ QueryStringField="ID" ] like:

    <asp:DataPager ID="DataPagerProducts" runat="server" QueryStringField="ID" PageSize="3">
                                <Fields>
                                    <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                                    <asp:NumericPagerField />
                                    <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
                                </Fields>
                            </asp:DataPager>
    

    Note: if not woking, then set also [ PagedControlID="ListView_Name" ].

    0 讨论(0)
  • 2021-02-07 05:45

    Also, if the data source of your ListView is changed (e.g. if displaying data based on search parameters), don't forget to reset the pager every time the data source is updated. With a ListView this is not as straightforward as some other data-bound controls (e.g. GridView):

    private void ResetListViewPager()
    {
        DataPager pager = (DataPager)ListViewMembers.FindControl("DataPager1");
        if (pager != null)
        {
            CommandEventArgs commandEventArgs = new CommandEventArgs(DataControlCommands.FirstPageCommandArgument, "");
            // MAKE SURE THE INDEX IN THE NEXT LINE CORRESPONDS TO THE CORRECT FIELD IN YOUR PAGER
            NextPreviousPagerField nextPreviousPagerField = pager.Fields[0] as NextPreviousPagerField;
            if (nextPreviousPagerField != null)
            {
                nextPreviousPagerField.HandleEvent(commandEventArgs);
            }
    
            // THIS COMMENTED-OUT SECTION IS HOW IT WOULD BE DONE IF USING A NUMERIC PAGER RATHER THAN A NEXT/PREVIOUS PAGER
            //commandEventArgs = new CommandEventArgs("0", "");
            //NumericPagerField numericPagerField = pager.Fields[0] as NumericPagerField;
            //if (numericPagerField != null)
            //{
            //    numericPagerField.HandleEvent(commandEventArgs);
            //}
        }
    }
    
    0 讨论(0)
提交回复
热议问题