0

我有一个带有 DataPager 的 ListView,它在 UpdatePanel 中加载数据源“A”。数据源“A”在 Page_Load 上加载。

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

ListView 外部的 OnClick 事件可以将 ListView 数据源更改为“B”;并在“A”和“B”之间切换。但是,DataPager 继续只使用数据源“A”。

如何让 DataPager 切换到选定的 ListView 数据源?

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>

背后的代码

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);
    }
}
4

1 回答 1

0

问题是您依赖于m_ViewBy在回发之间保留其价值,而这不会发生。最简单的解决方案是将该变量存储在 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);
    }
}
于 2009-11-17T17:38:03.147 回答