title: Two asp:ListView with one asp:datapager for each
body: <p>I'm trying to code a WebPart that gathers all the announcements on every site you are a contributing member of, then display them.  I have to display them in two listviews: Upcoming and Past.</p>
  
  <p>I have all working...  It gets all the proper announcements and puts them in the proper list view.  The Upcoming list view shows all upcoming announcements and the past livst view shows all past announcements.</p>
  
  <p>The problem I'm having is:  We want to have each list view showing only 10 items with a datapager to page for more.</p>
  
  <p>When I add two datapagers, each pointing to the its respective list, it does show the 1-10, but the the arros to display more do not work.  In fact, it ends up displaying funcky:</p>
  
  <blockquote>
    <p>After clicking: &lt; > &lt; 1 - 10 ></p>
    
    <p>Click again: 
    &lt; &lt; > > &lt; 1 - 10 ></p>
  </blockquote>
  
  <p>ascx file:</p>
  
  <pre><code>&lt;div class="announcement-upcming"&gt;
          &lt;asp:ListView ID="UpcmingListView" ItemPlaceholderID="UpcmingItem" OnItemDataBound="UpcmingItemDataBound"
              runat="server"&gt;
              &lt;LayoutTemplate&gt;
                  &lt;ul class="aggregation announcement upcming"&gt; 
                  &lt;asp:PlaceHolder runat="server" ID="UpcmingItem"&gt;&lt;/asp:PlaceHolder&gt;
                  &lt;/ul&gt;
              &lt;/LayoutTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;li class="item announcement upcming" id="liItem" runat="server"&gt;&lt;a id="aLink" class="item-title" runat="server"&gt;&lt;/a&gt;
                      &lt;div style="font-size: 80%"&gt;
                          &lt;a id="aSiteLink" target="_blank" runat="server"&gt;&lt;/a&gt;
                      &lt;/div&gt;
                      &lt;div id="expireDate" runat="server" style="font-size: 80%"&gt;&lt;/div&gt;
  
                  &lt;/li&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:ListView&gt;
          &lt;asp:Panel ID="pnlEmptyUpcming" CssClass="no-items" Visible="false" runat="server"&gt;
              &lt;ul class="aggregation announcement upcming"&gt;
                  &lt;li&gt;You have no upcoming announcements. &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/asp:Panel&gt;
          &lt;center&gt;
              &lt;asp:datapager id="UpcmingListingDataPager" pagedcontrolid="UpcmingListView" runat="server"&gt;
                  &lt;fields&gt;&lt;/fields&gt;
              &lt;/asp:datapager&gt;
          &lt;/center&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Code behind (ascx.cs file):</p>
  
  <pre><code>private void BuildUpcmingPagingControlFields()
  {
      PagingControlTemplate pagingControl = new PagingControlTemplate();
      TemplatePagerField field = new TemplatePagerField();
      field.PagerTemplate = pagingControl;
  
      UpcmingListingDataPager.PageSize = NumberOfUpcmingItems;
  
      if (UpcmingListingDataPager.PageSize &lt; (UpcmingListingDataPager.TotalRowCount + 1))
      {
          UpcmingListingDataPager.Fields.Add(pagingControl.GetBegginingPagerField());
          UpcmingListingDataPager.Fields.Add(field);
          UpcmingListingDataPager.Fields.Add(pagingControl.GetEndingPagerField());
      }
      else
      {
          UpcmingListingDataPager.Fields.Add(field);
      }
  
          UpcmingListView.DataBind();
          UpcmingListView.PagePropertiesChanged += UpcmingListing_PagePropertiesChanged;
  }
  
  protected void UpcmingListing_PagePropertiesChanged(object sender, EventArgs e)
  {
      this.UpcmingListView.DataBind();
  }
  
  protected void UpcmingItemDataBound(object obj, ListViewItemEventArgs ea)
  {
      try
      {
          DatedItem item = (DatedItem)(((ListViewDataItem)ea.Item).DataItem);
          HtmlControl li = (HtmlControl)ea.Item.FindControl("liItem");
          HtmlAnchor announcementAnchor = (HtmlAnchor)ea.Item.FindControl("aLink");
          HtmlAnchor siteAnchor = (HtmlAnchor)ea.Item.FindControl("aSiteLink");
          HtmlGenericControl expireAnchor = (HtmlGenericControl)ea.Item.FindControl("expireDate");
  
          DateTime ItemDate = item.Date;
          DateTime Tomorrow = DateTime.Now.AddDays(1);
  
          announcementAnchor.InnerText = item.Title;
  
          Uri SiteUri = new Uri(item.Url);
          if (item.Url.Contains("/Lists"))
          {
              siteAnchor.HRef = SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Lists"));
              using (SPSite announceSite = new SPSite(SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Lists"))))
              {
                  siteAnchor.InnerText = announceSite.RootWeb.Title;
              }
          }
          else
          {
              siteAnchor.HRef = SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Announcements"));
              using (SPSite announceSite = new SPSite(SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Announcements"))))
              {
                  siteAnchor.InnerText = announceSite.RootWeb.Title;
              }
          }
  
          if ((ItemDate == null) || (ItemDate == DateTime.MaxValue))
          {
              expireAnchor.InnerText = "Never expires";
          }
          else if ((ItemDate.Year == Tomorrow.Year) &amp;&amp; (ItemDate.Month == Tomorrow.Month) &amp;&amp; (ItemDate.Day == Tomorrow.Day))
          {
              expireAnchor.InnerText = "Expires tomorrow";
          }
          else
          {
              expireAnchor.InnerText = "Expires " + item.Date.ToString("MMMM d, YYYY");
          }
  
          announcementAnchor.InnerText = item.Title;
          announcementAnchor.HRef = item.Url;
          announcementAnchor.Attributes.Add("onclick", string.Format("return OpenDialog('{0}');", item.Url));
      }
      catch (Exception ex)
      {
          Logger.DefaultLogger.LogToOperations(ex);
      }
  }
  
  protected void PopulateUpcmingAnnouncements(List&lt;DatedItem&gt; Announcements)
  {
      if (Announcements != null)
      {
          List&lt;DatedItem&gt; upcmingAnnouncements = Announcements.Where(i =&gt; i.Date &gt;= DateTime.Today).ToList();
          upcmingAnnouncements = upcmingAnnouncements.OrderBy(t =&gt; t.Date).ToList();
  
          if (upcmingAnnouncements.Count == 0)
          {
              pnlEmptyUpcming.Visible = true;
              UpcmingListView.Visible = false;
              UpcmingListingDataPager.Visible = false;
          }
  
          UpcmingListView.DataSource = upcmingAnnouncements;
          UpcmingListView.ItemDataBound += UpcmingItemDataBound;
          UpcmingListView.DataBind();
          NumberOfUpcmingItems = upcmingAnnouncements.Count;
      }
      else
      {
          pnlEmptyUpcming.Visible = true;
          UpcmingListView.Visible = false;
          UpcmingListingDataPager.Visible = false;
      }
  }
  
  protected override void OnPreRender(EventArgs e)
  {
      base.OnInit(e);
      try
      {
          {{{Code that gets announcements and places the in TheseAnnouncements}}}
  
          PopulateUpcmingAnnouncements(TheseAnnouncements);
          PopulatepstAnnouncements(TheseAnnouncements);
      }
      catch (Exception ex)
      {
          Logger.DefaultLogger.LogToOperations(ex);
      }
  }
  </code></pre>