很抱歉,如果这篇文章看起来很冗长。
我有一个带有嵌套转发器的父转发器。
我的问题是我正在尝试使用'Next'和'Previous'按钮重新绑定我的转发器(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"> </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">< Previous week</asp:LinkButton>|
<asp:LinkButton id="lnkNextWeek" runat="server" commandargument="Next" cssclass="active" >Next week ></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
如何管理这些条件下的“下一步”和“上一页”链接。
非常感谢
答案 0 :(得分:2)
您确定需要四次下一个/上一个按钮吗?
无论如何,在rptTabContent_ItemDataBound上进行按钮管理。在ItemCommand上,您将获得新的起点,获取数据并进行数据绑定。在ItemDataBound上,您将获得“下一步”按钮和“上一步”按钮,并根据当前日期启用或禁用它们。应该在标记中禁用这两个按钮作为默认状态(对于没有数据的情况)。
这是一个模拟四个项目数据源的工作示例。
页
<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">< Previous week</asp:LinkButton> |
<asp:LinkButton id="lnkNextWeek" Enabled="false" runat="server" commandargument="Next" cssclass="active" >Next week ></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();
}