views:

1218

answers:

1

Hi,

I am using a ListView and DataPager in my web project for pagination. It works fine, but the generated HTML for the pagination is simply a span containing some hyperlinks.

I wanted to customize the HTML and display the links in an unordered list instead (ul).

Anybody know how this can be done? One way I can think of is CSSFriendly adapters, but I don't want to do that if there is an easier way.

Edit: Could somebody help me out with the exact steps required to create the controls in the template? Sorry for being daft, but I can't figure this part out, in spite of extensive Googling.

A: 

You can use the PagerTemplate to designate the markup you'd like to use for the paging control. I'm not sure exactly what you're trying to do in terms of displaying the paging information as as ul/li, but this should be enough to start you on the right track. Sorry for the code running long to the side...

ex:

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="gridInvoiceHistory"
            PageSize="20">
            <Fields>
                <asp:TemplatePagerField>
                    <PagerTemplate>
                        Page
                        <asp:Label runat="server" ID="labelCurrentPage" Text="<%# Container.TotalRowCount > 0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                        of
                        <asp:Label runat="server" ID="labelTotalPages" Text="<%#  Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                    </PagerTemplate>
                </asp:TemplatePagerField>

EDIT: here is a more detailed beginning to a solution for this:

<asp:TemplatePagerField>
     <PagerTemplate>
          <asp:BulletedList ID="listPages" runat="server" 
               DisplayMode="HyperLink" onclick="listPages_Click">
          </asp:BulletedList> 
     </PagerTemplate>
</asp:TemplatePagerField>

And here is what you would have in the code-behind:

protected void listPages_Click(object sender, BulletedListEventArgs e)
        {
            var pageNo = int.Parse((sender as BulletedList).Items[e.Index].Text);
            var startIndex = (pageNo - 1) * DataPager1.PageSize;
            DataPager1.SetPageProperties(startIndex, DataPager1.PageSize, true);
        }

What remains for you to do is to perform a databinding on the bulleted list against a method that gets the page count and returns an IEnumerable list of the text you want for the page links. Standard warning: this is sample code, and probably shouldn't be used in a production environment without a thorough vetting! :)

Josh E
Hi, I had tried using the TemplatePagerField, but that appears only to be used for displaying information like # of pages, current page, etc. I don't think it can be used to customize the page links created?
Wild Thing
Sure it can. Use the TemplatePagerField's OnPagerCommand event to do click processing. In your pager template, have either have an <ul><li><asp:LinkButton...> or an <asp:BulletedList> control to display your links. In your code-behind event handler, the DataPagerCommandEventArgs has the (arbitrary) CommandName (see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datapagercommandeventargs.aspx) to handle your click events
Josh E
Hi, I've been trying to take your suggestion and use it to create the links, but can't figure out how to do so. I created a template with the <ul><li><asp:LinkButton>. But how do I get it to create the links in the template? What are the placeholders for this? Any help would be much appreciated
Wild Thing
please see my edit for more info
Josh E
crap! forgot to include this link where you can find more info on the DataList and DataPager controls: http://www.4guysfromrolla.com/articles/122607-1.aspx (Part 1 of 8 or so)
Josh E
Thanks Josh - took me some time to figure out how to bind the bulletedlist because of a weird bug with the template control which I eventually found the solution for in the links you'd provided. Thanks!
Wild Thing