title: Dynamically generate page linkbuttons for the PagerTemplate of a GridView
body: <p>From the MSDN page for the <code>PagerTemplate</code> of the <code>GridView</code> control (emphasis mine):</p>
  
  <blockquote>
    <p>Typically, <strong>button controls</strong> are added to the pager template to perform the paging operations. The GridView control performs a paging operation when a button control with its CommandName property set to "Page" is clicked. The button's <strong>CommandArgument</strong> property determines the type of paging operation to perform.</p>
    
    <ul>
    <li>"Next": Navigates to the next page.</li>
    <li>"Prev": Navigates to the previous page.</li>
    <li>"First": Navigates to the first page.</li>
    <li>"Last": Navigates to the last page.</li>
    <li><strong>Integer value</strong>: Navigates to the specified page number.</li>
    </ul>
  </blockquote>
  
  <p>Source: <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(v=vs.110).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(v=vs.110).aspx</a></p>
  
  <p>This stuff is pretty straightforward for the next/prev/first/last buttons because of their static nature.</p>
  
  <pre><code>&lt;PagerTemplate&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;asp:Button CommandName="Page" CommandArgument="First" Enabled="&lt;%# Model.PageContext.HasPrevious %&gt;" Text="First" runat="server" /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Button CommandName="Page" CommandArgument="Prev" Enabled="&lt;%# Model.PageContext.HasPrevious %&gt;" Text="Previous" runat="server" /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Button CommandName="Page" CommandArgument="Next" Enabled="&lt;%# Model.PageContext.HasNext %&gt;" Text="Next" runat="server" /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:Button CommandName="Page" CommandArgument="Last" Enabled="&lt;%# Model.PageContext.HasNext %&gt;" Text="Last" runat="server" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/PagerTemplate&gt;
  </code></pre>
  
  <p>On the other hand, the <code>CommandArgument</code> for numeric buttons has to be dynamic and unique for each page that can be navigated to. I'm guessing I will need a <code>for</code>-loop or a repeater control to get the right number of page links on the user's display.</p>
  
  <p>Nothing I've tried seems to <em>just</em> work. My for-loop code doesn't even compile.</p>
  
  <pre><code>&lt;% for (int pageIndex = 0; pageIndex &lt; Model.PageContext.PageCount; pageIndex++) { %&gt;
      &lt;asp:LinkButton CommandName="Page" CommandArgument="&lt;%= pageIndex %&gt;" Text="&lt;%= pageIndex + 1 %&gt;" runat="server"/&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>My alternative approach uses a <code>Repeater</code>control and <em>does</em> compile, but the <code>Repeater</code>control itself handles the <code>ItemCommand</code> for each button, preventing the "Page" <code>ItemCommand</code> events from bubbling up to the <code>GridView</code>.</p>
  
  <pre><code>&lt;asp:Repeater ItemType="System.Int32" SelectMethod="GetPages" runat="server"&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:LinkButton CommandName="Page" CommandArgument="&lt;%# Item %&gt;" Text="&lt;%# Item + 1 %&gt;" runat="server" /&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Each button raises the correct event, but the events never reach the <code>GridView</code> because they are handled at a lower level by the <code>Repeater</code> control. I have to attach an event handler that listens for <code>RepeaterCommandEventArgs</code> and then set the new page index on the <code>GridView</code> myself.</p>
  
  <p><em>*Takes deep breath*</em></p>
  
  <p>Can I add numeric page buttons <strong>without having to wire up events myself</strong>?</p>
  
  <p>The result that I'm trying to achieve, based on the code above:</p>
  
  <p><img src="https://i.stack.imgur.com/Z6U0A.png" alt="linkbuttons"></p>