【问题标题】:Rebind Repeater using Next / Previous buttons in the ItemTemplate of the same Repeater使用同一中继器的 ItemTemplate 中的下一个/上一个按钮重新绑定中继器
【发布时间】:2015-05-01 17:07:06
【问题描述】:

对不起,如果这篇文章看起来很啰嗦。

我有一个带有嵌套中继器的父中继器。

我的问题是我正在尝试使用“下一个”和“上一个”按钮重新绑定我的转发器 (rptTabContent),因此内部转发器也是如此,但是当我尝试在 ItemCommand 事件期间操纵这些链接的状态时,它们是被绑定覆盖。

因此,如果在随后单击“下一个”或“上一个”链接后没有数据,我将无法禁用或启用按钮,这是我正在尝试做的。

嵌套的重复器在 7 列中显示一周的数据。下一个按钮用于显示下一周的数据和前一周的数据。最初,前一个按钮是不活动的。

再次绑定父中继器,因为最初它循环出 4 个 div,每个 div 包含一个数据表。如果在一个中继器上按下一个下一步按钮,则所有表格都必须显示其接下来 7 天的数据。

每个转发器都有完全相同数量的项目。

最初在页面加载时正确绑定所有数据。

我知道我的收藏中第一项和最后一项的日期,因此我能够计算我需要绑定到我的父中继器的对象的日期范围。

HTML 如下

        <asp:Repeater ID="rptTabContent" runat="server" OnItemCommand="rptTabContent_ItemCommand">
        <ItemTemplate>
            <div id="<%# Eval("SlotTypeUrl") %>" class="delivery-timetable-container">
                <table cellpadding="0" cellspacing="0" border="0">
                    <asp:Repeater ID="rptDeliverySlots" runat="server">
                        <ItemTemplate>
                            <tr class="time-slots">
                                <th><asp:Label id="lblRowTime" runat="server"></asp:Label></th>
                                <asp:Repeater ID="rptDeliverySlot" runat="server">
                                    <ItemTemplate>
                                        <td id="tdDay" runat="server">
                                            <cc1:RepeaterRadioButton id="rptrdoDeliverySlot" groupname="del-times" runat="server" />
                                        </td>           
                                    </ItemTemplate> 
                                </asp:Repeater>                            
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                    <tr>
                        <td colspan="8">&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan="4">            
                                <div class="eco-rollover">
                                    <div>
                                        <img src="icon.gif" />
                                    </div>
                                </div>
                        </td>
                        <td colspan="4">
                            <asp:LinkButton id="lnkPreviousWeek" enabled="false"  runat="server" commandargument="Previous" cssclass="inactive">&lt; Previous week</asp:LinkButton>|
                            <asp:LinkButton id="lnkNextWeek" runat="server" commandargument="Next" cssclass="active" >Next week &gt;</asp:LinkButton>
                        </td>
                    </tr>
                </table>
            </div>
        </ItemTemplate>
    </asp:Repeater>

我的 ItemCommand 事件在这里:

Protected Sub rptTabContent_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) 
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)
        Dim filteredProposedDeliveries  As New List(Of ProposedDeliverySlotDTO)
        Dim firstDate, lastDate As Date
        Dim startDate, endDate  As Date

        If (Session("FirstDeliveryDate") IsNot Nothing) AndAlso (Session("LastDeliveryDate") IsNot Nothing) Then

            firstDate = CType(Session("FirstDeliveryDate"), Date)
            lastDate  = CType(Session("LastDeliveryDate"),  Date) 

            Select Case e.CommandArgument.ToString()
                Case "Next"    
                    'Get new startDate using firstDate and store to use next time
                    'disable next button if startDate > lastDate
                Case "Previous"
                    'Get new startDate from current displayed date and overright stored startdate
                    'disable previous button if startDate < firstDate
            End Select

            endDate = startDate.AddDays(7)     

        End If

        'filteredProposedDeliveries = Get object between startDate and EndDate

        Dim slots As List(Of SlotType)  = PrepareSlotsForBinding(filteredProposedDeliveries)            

        Dim rptTabContent As Repeater = CType(e.Item.BindingContainer, Repeater)
        rptTabContent.DataSource = slots
        rptTabContent.DataBind()
    End If
End Sub

如何在这些条件下管理我的“下一个”和“上一个”链接。

非常感谢

【问题讨论】:

    标签: asp.net vb.net repeater


    【解决方案1】:

    您确定需要四次下一个/上一个按钮吗?

    无论如何,请在 rptTabContent_ItemDataBound 上进行按钮管理。在 ItemCommand 上,您可以获得新的起点,获取数据并进行数据绑定。在 ItemDataBound 上,您将获得 Next 按钮和 Previous 按钮,并根据当前日期启用或禁用它们。这两个按钮都应该在标记中作为默认状态禁用(当没有数据时)。

    这是一个模拟四个项目的数据源的工作示例。

    页面

    <asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <div>
                Label: <asp:Label ID="itemLabel" runat="server"></asp:Label><br />
                <asp:LinkButton id="lnkPreviousWeek" Enabled="false" runat="server" commandargument="Previous" cssclass="inactive">&lt; Previous week</asp:LinkButton>&nbsp;|&nbsp;
                <asp:LinkButton id="lnkNextWeek" Enabled="false" runat="server" commandargument="Next" cssclass="active" >Next week &gt;</asp:LinkButton>
                <hr />
            </div>
        </ItemTemplate>
    </asp:Repeater>
    

    代码

    private int current = 1;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["current"] == null)
            ViewState["current"] = current;
        else
            current = (int)ViewState["current"];
    
        if (!IsPostBack)
        {
            Repeater1.DataSource = GetData(current);
            Repeater1.DataBind();
        }
    }
    
    private List<int> GetData(int start)
    {
        //pseudo data; simulates a page of 2 records
        List<int> ds = new List<int>();
        ds.Add(start);
        ds.Add(start + 1);
    
        return ds;
    }
    
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if ((string)e.CommandArgument == "Next")
            current++;
        else if ((string)e.CommandArgument == "Previous")
            current--;
    
        ViewState["current"] = current;
    
        Repeater1.DataSource = GetData(current);
        Repeater1.DataBind();
    }
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton prev = (LinkButton)e.Item.FindControl("lnkPreviousWeek");
        LinkButton next = (LinkButton)e.Item.FindControl("lnkNextWeek");
    
        //here 1 and 3 are artificial boundaries for data for the sake of example
        if (current > 1)
            prev.Enabled = true;
        if (current < 3)
            next.Enabled = true;
    
        Label label = (Label)e.Item.FindControl("itemLabel");
        label.Text = e.Item.DataItem.ToString();
    }
    

    【讨论】:

    • 非常感谢您的回复。自发布以来,我一直在努力解决这个问题,并按照您的建议实施了相同的操作。感谢您回答这是一个很长的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多