I am building a website using the Twitter Bootstrap and ASP.Net C# Webforms. I have a ListView on my page with a DataPager bound to it, but I need to change the way .Net renders the HTML of the DataPager.
Currently, all pager items are displaying like this:
<div class="clearfix pagination pagination-centered"> <span id="cpBody_dpListing"> <a class="aspNetDisabled">First</a> <span>1</span> <a href="javascript:__doPostBack('ctl00$cpBody$dpListing$ctl02$ctl01','')">2</a> <a href="javascript:__doPostBack('ctl00$cpBody$dpListing$ctl03$ctl00','')">Last</a> </span> </div>
however I need to wrap all my items in an unordered list rather than span and a tags. My current mark-up looks like this:
<div class="clearfix pagination pagination-centered">
<asp:DataPager ID="dpListing" runat="server" PagedControlID="lvListing" PageSize="10" OnPreRender="dpListing_PreRender">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<asp:BulletedList ID="listPages" runat="server" DisplayMode="LinkButton" OnClick="listPages_Click"></asp:BulletedList>
</PagerTemplate>
</asp:TemplatePagerField>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" />
<asp:NumericPagerField PreviousPageText="< Prev 10" NextPageText="Next 10 >" ButtonCount="10" />
<asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
I somehow need to override the NextPreviousPagerField and NumericPagerField so they output <li> tags rather than <span> and <a>.
I have a slightly different solution to this. Instead of changing the rendering of the DataPager, I have slightly adapted the markup, and in terms of rendering, it still looks "bootstrap-like", even though it doesn't have the UL/LI tags with pagination class.
A very important thing (as explained in https://stackoverflow.com/a/19398488/1948625) is changing the class asp.net outputs for disabled control, it defaults to "aspNetDisabled" but for bootstrap it's much better to simply use "disabled". Do this in Application_Start in the Global.asax. If you don't, the first page, previous page, next page and last page buttons don't appear disabled.
Source: WebControl.DisabledCssClass Property (MSDN)
Asp.net server markup above renders as this pager:
And this is the rendered html:
The
DataPager
doesn't support this out of the box, so you're going to need a custom control. Fortunately, it's quite east to implement!For each
DataPagerField
, theDataPager
adds aDataPagerFieldItem
control to its own control collection, and then tells the field to create its controls within that item. The built-in fields will add non-breaking spaces between the buttons (unless you set theRenderNonBreakingSpacesBetweenControls
property tofalse
), but they're quite easy to identify and suppress.This control will still render the
<a>
tags for the enabled buttons and the<span>
tag for the current page number, but should be close to what you need:HTML output:
I've made additional changes to Andreas's answer. The Bootstrap samples wrap the current page (active) page inside of a hyperlink tag, so I've noticed that some Bootstrap templates do not format the active tag correctly when choosing a size variation such as pagination-lg or pagination-sm. Here is my version of RenderContents with the extra hyperlink wrap added:
In addition to the answer of Richard ... I used his approach and added support for the "disabled" and "active" classes and the surrounding div-Tag-
Update: I added the suggestion of John for special rendering of the active-Label as a hyperlink for correct rendering of pagination-sm and pagination-lg. So credit and thanks to John too.
Update 2: Added rendering of id for the control. Thanks to DGibbs.