title: How to to get a DataPager to switch to new ListView datasource?
body: <p>I have a single ListView with a DataPager that loads datasource 'A' within an UpdatePanel. Datasource 'A" is loaded on Page_Load.</p>
  
  <pre><code>if (!IsPostBack)
  {
      GetBookmarks(0);
  }
  </code></pre>
  
  <p>An OnClick event outside the ListView can change the ListView datasource to 'B'; and toggle between 'A' and 'B'. However the DataPager continues to only use datasource 'A'.</p>
  
  <p>How can I get the DataPager to switch to the selected ListView datasource?</p>
  
  <p>ASPX</p>
  
  <pre><code>&lt;asp:ListView ID="lvBookmarks" OnPagePropertiesChanged="lv_PagePropertiesChanged" OnPagePropertiesChanging="lv_PagePropertiesChanging" runat="server" Visible="true"&gt;
                  &lt;LayoutTemplate&gt;
                      &lt;div class="datapager_top"&gt;
                          &lt;div class="datapager_pagesize"&gt;
                              &lt;asp:DataPager ID="dpBookmarksPageSizeTop" PagedControlID="lvBookmarks" PageSize="2" runat="server"&gt;
                                  &lt;Fields&gt;
                                      &lt;asp:TemplatePagerField&gt;
                                          &lt;PagerTemplate&gt;
                                              Displaying &lt;asp:Literal ID="PageSizeBegin" runat="server" Text="&lt;%# Container.StartRowIndex + 1%&gt;" /&gt; to &lt;asp:Literal ID="PageSizeEnd"  runat="server" Text="&lt;%# Container.StartRowIndex + Container.PageSize &gt; Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize %&gt;" /&gt; of &lt;asp:Literal ID="TotalItems" runat="server" Text="&lt;%# Container.TotalRowCount %&gt;" /&gt; items
                                          &lt;/PagerTemplate&gt;
                                      &lt;/asp:TemplatePagerField&gt;
                                  &lt;/Fields&gt;
                              &lt;/asp:DataPager&gt;
                          &lt;/div&gt;
                          &lt;div class="datapager_links"&gt;
                              &lt;asp:DataPager ID="dpBookmarksTop" PagedControlID="lvBookmarks" PageSize="2" runat="server"&gt;
                                  &lt;Fields&gt;
                                      &lt;asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" FirstPageText="&amp;#8249; First" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false"  /&gt;
                                      &lt;asp:NumericPagerField ButtonCount="5" CurrentPageLabelCssClass="datapager_currentpage" NextPageText="&amp;#8250;&amp;#8250;" NextPreviousButtonCssClass="datapager_nextprevious" PreviousPageText="&amp;#8249;&amp;#8249;" RenderNonBreakingSpacesBetweenControls="false" /&gt;
                                      &lt;asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" LastPageText="Last &amp;#8250;" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" /&gt;
                                  &lt;/Fields&gt;
                              &lt;/asp:DataPager&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;table class="table"&gt;
                          &lt;tr id="itemPlaceholder" runat="server"&gt;&lt;/tr&gt;
                      &lt;/table&gt;
                      &lt;div class="datapager_btm"&gt;
                          &lt;div class="datapager_links"&gt;
                              &lt;asp:DataPager ID="dpBookmarksBtm" PagedControlID="lvBookmarks" PageSize="2" runat="server"&gt;
                                  &lt;Fields&gt;
                                      &lt;asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" FirstPageText="&amp;#8249; First" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false"  /&gt;
                                      &lt;asp:NumericPagerField ButtonCount="5" CurrentPageLabelCssClass="datapager_currentpage" NextPageText="&amp;#8250;&amp;#8250;" NextPreviousButtonCssClass="datapager_nextprevious" PreviousPageText="&amp;#8249;&amp;#8249;" RenderNonBreakingSpacesBetweenControls="false" /&gt;
                                      &lt;asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" LastPageText="Last &amp;#8250;" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" /&gt;
                                  &lt;/Fields&gt;
                              &lt;/asp:DataPager&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/LayoutTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;tr class='&lt;%# Container.DataItemIndex % 2 == 0 ? "row" : "altrow" %&gt;' id="row" runat="server"&gt;
                          &lt;td style="width:20px;"&gt;&lt;img alt="" height="16" src="/include/graphic/icon/bookmark_document.png" width="16" /&gt;&lt;/td&gt;
                          &lt;td style="width:220px;"&gt;&lt;a href="&lt;%# Eval("Link") %&gt;"&gt;&lt;%# Eval("LinkTitle") %&gt;&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;%# Eval("Description") %&gt;&lt;/td&gt;
                          &lt;td style="width:20px;"&gt;&lt;asp:CheckBox ID="cbMyBookmark" runat="server" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>Code behind</p>
  
  <pre><code>protected Int32 m_ViewBy;
  
  protected void GetCommonBookmarks_Click(object sender, EventArgs e)
  {
      m_ViewBy = 0;
      GetBookmarks(m_ViewBy);
  }
  
  protected void GetMyBookmarks_Click(object sender, EventArgs e)
  {
      m_ViewBy = 1;
      GetBookmarks(m_ViewBy);
  }
  
  protected void GetBookmarks(Int32 i)
  {
      m_ViewBy = i;
      ltlTemp.Text = "";
  
      if (i == 0)
      {
          ltlGvTitle.Text = "Common bookmarks";
  
          lvBookmarks.Items.Clear();
  
          List&lt;Bookmark&gt; lCommonBookmark = CNETLib.GetBookmarkList();
  
          String[] sArray = { "BID" };
  
          lvBookmarks.DataKeyNames = sArray;
  
          lvBookmarks.DataSource = lCommonBookmark;
          lvBookmarks.DataBind();
      }
      else
      {
          DataSet ds = new DataSet();
          DataTable dt = new DataTable();
          DBUser dbuser = CNetCache.CurrentUser();
  
          ltlGvTitle.Text = "My bookmarks";
  
          lvBookmarks.Items.Clear();
  
          dt.Load(dbuser.GetBothBookmarks());
          ds.Tables.Add(dt);
  
          String[] sArray = { "BMID" };
  
          lvBookmarks.DataKeyNames = sArray;
          lvBookmarks.DataSource = ds;
          lvBookmarks.DataBind();
      }
  }
  
  protected void lv_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  {
      DataPager dpBookmarksPageSizeTop = (DataPager)lvBookmarks.FindControl("dpBookmarksPageSizeTop");
      DataPager dpBookmarksTop = (DataPager)lvBookmarks.FindControl("dpBookmarksTop");
      DataPager dpBookmarksBtm = (DataPager)lvBookmarks.FindControl("dpBookmarksBtm");
  
      dpBookmarksPageSizeTop.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
      dpBookmarksTop.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
      dpBookmarksBtm.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
  }
  
  protected void lv_PagePropertiesChanged(object sender, EventArgs e)
  {
      if (m_ViewBy == 1)
      {
          GetBookmarks(1);
  
      }
      else
      {
          GetBookmarks(0);
      }
  }
  </code></pre>