views:

1497

answers:

1

Sorry if this post appears to be long winded.

I have a parent repeater with nested repeaters.

My problem is I am trying to use 'Next' and 'Previous' buttons to rebind my repeater (rptTabContent) and thus the inner repeaters also, but when I try to manipulate the state of these links during the ItemCommand event they are overwritten by the bind.

So I cannot disable or enable the buttons if there is to be no data after a consquent click of either 'Next' or 'Previous' links which is what I am trying to do.

The nested repeaters show a weeks worth of data in 7 columns. The next button is meant to show the next weeks data and previous the previous weeks data. Initially the previous button is inactive.

The parent repeater is bound again because initially it loops out 4 divs each containing a table of data. If one next button is hit on one repeater all tables must show their next 7 days of data.

Each repeater has the exact same number of items.

Initially on page load all of the data bind correctly.

I know the date of the very first item and last item in my collection so I am able to calculate the date range of the objects I need to bind to my parent repeater.

The HTML is below

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

My ItemCommand event is here:

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

How can I manage my 'Next' and 'Previous' links under these conditons.

Many thanks

+2  A: 

Are you sure you need next/previous buttons four times?

Anyway, do your button management on rptTabContent_ItemDataBound. On ItemCommand, you get new starting point, get data and do data bind. On ItemDataBound, you get the Next button and the Previous button and enable or disable them based on what current date is. Both buttons should be disabled in the markup as a default state (for when there is no data).

Here is a working sample that simulates a data source of four items.

Page

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

Code

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();
}
Ruslan
Thanks very much for the reply. I have been working on this since posting and have implemented the same actions to what you have suggested. Appreciate you answering it is a long post.
w4ymo