Asp.net WebForms Repeater OnClick在转发器内部时不会触发

时间:2018-05-23 18:58:13

标签: c# asp.net webforms

回到故事,我重新发明分页来处理数据源调用,我只能真正获得结果数和当前页面。我知道我在哪个页面,我知道有多少页面。我想将转发器与当前所选项目始终放在中间,这样我就可以在每次回发时重建列表。出于某种原因,我的点击事件都没有被触发。网站上的其他答案通过声明“不要在回发时重建列表”来解决这个问题。",但这正是我需要在这里做的。

我的ascx代码如下所示:

        <asp:Repeater ID="PaginationRepeater" runat="server"
            OnItemDataBound="PaginationRepeater_ItemDataBound">
            <ItemTemplate>
                <li>
                    <asp:LinkButton ID = "PaginationLink" runat="server" 
                        Text = '<%#Container.DataItem.ToString()%>'
                        CommandArgument = '<%#Container.DataItem.ToString()%>'
                        CommandName="WasClicked"
                        OnCommand="PaginationLink_Command"
                        OnClick="PaginationLink_Click">
                    </asp:LinkButton>
                </li>
            </ItemTemplate>
        </asp:Repeater>

游戏中的属性:

public int ResultCount { get; set; }
public bool ShowPaging { get; set; }
public int PreviousPageNumber { get; set; }
public int CurrentPageNumber { get; set; }
public List<int> PageNumbers { get; set; }
public int NextPageNumber { get; set; }

PaginationLink_Command和PaginationLink_Click命令暂时抛出一个未实现的异常。希望在某些时候我能够使用这些代码。

private void Link_Command(object sender, CommandEventArgs e)
{
    throw new NotImplementedException();
}
private void Link_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}
protected void PaginationLink_Command(object sender, CommandEventArgs e)
{
    throw new NotImplementedException();
}
protected void PaginationLink_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

希望有人在网上表现出色,愿意提供帮助。

更新:回答我如何将数据加载到转发器中:

public override void OnContentLoaded()
{
    base.OnContentLoaded();
    Page.LoadComplete += new EventHandler(Page_LoadComplete);
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
    int currentPage = 1; // TODO: get from pagination.
    int productCount = LoadProducts(currentPage);
    LoadPager(currentPage, productCount);
}

public void LoadPager(int currentPage, int resultCount)
{
    int itemsPerPage = 9;
    int displayedPages = 9;
    ShowPaging = resultCount > itemsPerPage;
    if (!ShowPaging)
        return;

    int maxPages = (int)Math.Ceiling((decimal)(resultCount / itemsPerPage)) + 1;
    if (currentPage > maxPages)
        currentPage = maxPages;

    int start = Enumerable.Max(new[] { currentPage - (displayedPages / 2), 1 });
    int end = Enumerable.Min(new[] { start + displayedPages, maxPages });

    CurrentPageNumber = currentPage;
    PreviousPageNumber = currentPage - 1 > 1 ? currentPage - 1 : 1;
    NextPageNumber = currentPage + 1 <= maxPages ? currentPage + 1 : maxPages;
    PageNumbers = Enumerable.Range(start, end - start + 1).ToList();

    PaginationRepeater.DataSource = PageNumbers;
    PaginationRepeater.DataBind();
}

TLDR是我在LoadComplete事件期间重新绑定的。

0 个答案:

没有答案