views:

445

answers:

2

I have a single ListView with a DataPager that loads datasource 'A' within an UpdatePanel. Datasource 'A" is loaded on Page_Load.

if (!IsPostBack)
{
    GetBookmarks(0);
}

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'.

How can I get the DataPager to switch to the selected ListView datasource?

ASPX

<asp:ListView ID="lvBookmarks" OnPagePropertiesChanged="lv_PagePropertiesChanged" OnPagePropertiesChanging="lv_PagePropertiesChanging" runat="server" Visible="true">
                <LayoutTemplate>
                    <div class="datapager_top">
                        <div class="datapager_pagesize">
                            <asp:DataPager ID="dpBookmarksPageSizeTop" PagedControlID="lvBookmarks" PageSize="2" runat="server">
                                <Fields>
                                    <asp:TemplatePagerField>
                                        <PagerTemplate>
                                            Displaying <asp:Literal ID="PageSizeBegin" runat="server" Text="<%# Container.StartRowIndex + 1%>" /> to <asp:Literal ID="PageSizeEnd"  runat="server" Text="<%# Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize %>" /> of <asp:Literal ID="TotalItems" runat="server" Text="<%# Container.TotalRowCount %>" /> items
                                        </PagerTemplate>
                                    </asp:TemplatePagerField>
                                </Fields>
                            </asp:DataPager>
                        </div>
                        <div class="datapager_links">
                            <asp:DataPager ID="dpBookmarksTop" PagedControlID="lvBookmarks" PageSize="2" runat="server">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" FirstPageText="&#8249; First" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false"  />
                                    <asp:NumericPagerField ButtonCount="5" CurrentPageLabelCssClass="datapager_currentpage" NextPageText="&#8250;&#8250;" NextPreviousButtonCssClass="datapager_nextprevious" PreviousPageText="&#8249;&#8249;" RenderNonBreakingSpacesBetweenControls="false" />
                                    <asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" LastPageText="Last &#8250;" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" />
                                </Fields>
                            </asp:DataPager>
                        </div>
                    </div>
                    <table class="table">
                        <tr id="itemPlaceholder" runat="server"></tr>
                    </table>
                    <div class="datapager_btm">
                        <div class="datapager_links">
                            <asp:DataPager ID="dpBookmarksBtm" PagedControlID="lvBookmarks" PageSize="2" runat="server">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" FirstPageText="&#8249; First" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false"  />
                                    <asp:NumericPagerField ButtonCount="5" CurrentPageLabelCssClass="datapager_currentpage" NextPageText="&#8250;&#8250;" NextPreviousButtonCssClass="datapager_nextprevious" PreviousPageText="&#8249;&#8249;" RenderNonBreakingSpacesBetweenControls="false" />
                                    <asp:NextPreviousPagerField ButtonCssClass="datapager_firstlastpage" LastPageText="Last &#8250;" RenderDisabledButtonsAsLabels="true" RenderNonBreakingSpacesBetweenControls="false" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" />
                                </Fields>
                            </asp:DataPager>
                        </div>
                    </div>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr class='<%# Container.DataItemIndex % 2 == 0 ? "row" : "altrow" %>' id="row" runat="server">
                        <td style="width:20px;"><img alt="" height="16" src="/include/graphic/icon/bookmark_document.png" width="16" /></td>
                        <td style="width:220px;"><a href="<%# Eval("Link") %>"><%# Eval("LinkTitle") %></a></td>
                        <td><%# Eval("Description") %></td>
                        <td style="width:20px;"><asp:CheckBox ID="cbMyBookmark" runat="server" /></td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>

Code behind

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<Bookmark> 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);
    }
}
A: 

Can you post some more code (specifically the aspx) and any event handlers in your code behind?

Dav Evans
A: 

The problem is that you are relying on m_ViewBy retaining its value between postbacks, which does not occur. The simplest solution is to store that variable in the Session.

protected void GetCommonBookmarks_Click(object sender, EventArgs e){
    Session("ViewBy") = 0;
    GetBookmarks(0);
}

protected void GetMyBookmarks_Click(object sender, EventArgs e){
    Session("ViewBy") = 1;
    GetBookmarks(1);
}

protected void lv_PagePropertiesChanged(object sender, EventArgs e){
    if (Session("ViewBy") == 1) {
        GetBookmarks(1);
    } else {
        GetBookmarks(0);
    }
}
Jason Berkan
Using a session works great. I appreciate your assistance.
Brian