Asp.Net: DataPager problem with Listview

by Pieter Brinkman 23. December 2009 06:48

When using the Datapager with a ListView I had the following problem. When clicking a paging button for the first time nothing happens.But when I click a button the second time, then the page from the first click loads.

I search the internet for a solution and found that you need to add some code to the OnPagePropertiesChanging event of the list view to reload the DataPager.

The following code is the solution to my problem. Including a fix that the data doesn't get loaded two times.

[code:c#]

private List<Product> productList;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        fillGrid();
}

private void fillGrid()
{
    if(productList == null)
        productList = getproducts();
    ListView1.DataSource = productList;
    ListView1.DataBind();
    DataPager1.DataBind();
}

public List<Product> getproducts()
{
    using (AdventureWrksDataContext db = new AdventureWrksDataContext())
    {
        return db.Products.ToList();
    }
}

protected void lvproducts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    fillGrid();
}

[/code]


You can download the solution (PagingExample.zip (83.05 kb))

Cheers,

Pieter

Tags: , ,

ASP.Net | Controls

Sitecore: itemlist with ObjectDataSource, Listview and DataPager

by Pieter Brinkman 8. April 2009 05:22

For a new project I started working with Sitecore again.

I needed to create a listing of items with paging. I implemented a Listview with a ObjectDataSource and the DataPager control.

 

<asp:ListView ID="lvListing" runat="server" DataSourceID="odsListing" onitemdatabound="lvListing_ItemDataBound">
    <ItemTemplate>
         <li class="clearfix">
            <a class="thumb" ID="thumnailLink" runat="server" > 
                <sc:Image Field="Thumbnail" ID="thumbnailImage" MaxWidth="120" runat="server" />
            </a>
            <h2><asp:HyperLink runat="server" ID="hlTitle" /></a></h2>
            <span class="date"> <sc:Date ID="listingDate" runat="server" Format="dd-MM-yyyy" /> <asp:Literal Visible="false" runat="server" ID="litDateField" /></span>
            <%# Eval("Fields[\"IntroText\"]") %>
            <asp:HyperLink runat="server" CssClass="url" ID="hlNavigationUrl" />
     </li>
    </ItemTemplate>
    <EmptyDataTemplate>
        Geen artikelen
    </EmptyDataTemplate>
    <LayoutTemplate>
        <ul ID="itemPlaceholderContainer" runat="server" style="">
            <li ID="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemSeparatorTemplate>
        <br />
    </ItemSeparatorTemplate>
</asp:ListView>

<ul>
  <li class="paging">
        <asp:DataPager ID="DataPager1" PagedControlID="lvListing" runat="server" PageSize="10" >
         <Fields>
             <asp:nextpreviouspagerfield ButtonCssClass="paging-previous" PreviousPageText="Vorige" ButtonType="Link"  ShowPreviousPageButton="True" ShowNextPageButton="False"/>
                <asp:nextpreviouspagerfield ButtonCssClass="paging-next" NextPageText="Volgende" ButtonType="Link" ShowPreviousPageButton="False" ShowNextPageButton="True" />
             <asp:NumericPagerField ButtonType="Link" NextPageText="VOLGENDE" PreviousPageText="VORIGE" RenderNonBreakingSpacesBetweenControls="true" />
         </Fields>
        </asp:DataPager>
    </li>
</ul> 

<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="GetItems"
        TypeName="MyNameSpace.MyUsercontrol"
        DataObjectTypeName="Sitecore.Collections.ChildList">
</asp:ObjectDataSource>

 

 


In the code behind I created the public method GetItems which returns a ChildListCollection.

namespace MyNameSpace
{
    public partial class MyUsercontrol: System.Web.UI.UserControl
    {
     
       public Sitecore.Collections.ChildList GetItems()
       {
          return Sitecore.Context.Item.GetChildren();
        }
     }


The PagedControlID property of the datapager control is set to the ID of the Listview.  

 

 

 

Tags: , , , ,

ASP.Net | Controls

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am a .NET Software Engineer for Achmea IT in De Meern, The Netherlands. My interests are mainly web applications created with ASP.NET, MSSQL and Silverlight.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Most comments

club penguin cheats club penguin cheats
4 comments
us United States
Web Design Company Web Design Company
2 comments
Web design Web design
2 comments
gb United Kingdom