Why is it so difficult to fire itemcommand events in a repeater?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Sadlier

    #1

    Why is it so difficult to fire itemcommand events in a repeater?

    I have a repeater with a image buttons in the footer for paging. I also have linkbuttons in the header for sorting.The sorting works beautifully but the footer does not fire off the itemcommand event.
    Code:
    <FooterTemplate>
      <tr>
        <td align="left" class="footer" colspan="10">
          &nbsp;
          <pagerstyle cssclass="footer" font-italic="True" />
          <pagertemplate></pagertemplate>
          <asp:Label ID="PageLabel" runat="server" Text='<%# GetPagingInfo() %>' Visible="true"/>
          &nbsp;
          <asp:ImageButton ID="Page_First" runat="server" CommandArgument="First" CommandName="Page" ImageUrl="~/images/first_page.gif" ToolTip="Back to First Page" />
          <asp:ImageButton ID="Page_Prev" runat="server" CommandArgument="Prev" CommandName="Page" ImageUrl="~/images/prev_page.gif" ToolTip="Back One Page" />
          <asp:ImageButton ID="Page_Next" runat="server" CommandArgument="Next" CommandName="Page" ImageUrl="~/images/next_page.gif" ToolTip="Forward One Page" />
          <asp:ImageButton ID="Page_Last" runat="server" CommandArgument="Last" CommandName="Page" ImageUrl="~/images/last_page.gif" ToolTip="Forward to Last Page" />
          &nbsp;
          <asp:ImageButton ID="PrintAllImageButton" runat="server" CommandName="PrintAll" ImageUrl="~/images/printer.gif" ToolTip="Print all pages" Width="20px" />
        </td>
      </tr>
    </FooterTemplate>

    The cod ebehind in the page_Load is
    Code:
     ar = new Pecos.AuditReview();
            if (!IsPostBack)
            {
                //ViewState["sortdir"] = "desc";
                ViewState["page"] = "1";
                StartAt = 1;
                dtefrom.MaxDate = DateTime.Now;
                dtefrom.MinDate = DateTime.Now.AddYears(-1);
                dtethru.MaxDate = DateTime.Now;
                dtethru.MinDate = DateTime.Now.AddYears(-1);
                dtefrom.SelectedDate = DateTime.Now.AddDays(-1);
                dtethru.SelectedDate = DateTime.Now;
                TableName = String.Empty;
            }
            
                if(ddlTablenames.SelectedIndex<=0) LoadTableNames();
           PageSize = Int32.Parse(Master.Pecos.Preferences["Default.PageSize"].ToString());
            ddlRecordIds.Visible = ddlTablenames.SelectedIndex>0?true:false;
            
            FromDate = dtefrom.SelectedDate.ToShortDateString();
            ToDate = dtethru.SelectedDate.ToShortDateString();
            Action = "sel";
         rptAuditActionsInformation.DataSource = ar.GetAuditRecordsCommand();
            rptAuditActionsInformation.DataBind();
            this.rptAuditActionsInformation.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptAuditActionsInformation_ItemCommand);
    Last edited by Frinavale; Oct 14 '10, 01:07 PM. Reason: Please put code in [code] ... [/code] tags. Added code tags and proper indentation.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You are performing a DataBind on your rptAuditActions Information in the Page Load event every time the page is loaded. This could be the problem.

    Try moving this code to the Page PreRender event instead.

    -Frinny

    Comment

    • Carl Sadlier

      #3
      I moved the datasource and databind methods to the preender event but the itemcommand still doesn't fire for the footer. The thing which confuses me is the sort fires every time from the header template btu not the footer. I use very similar code in another page which does work. I can't see what's different.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm not sure why you are having so many problems. I created a test project based on your footer and had no problems with this.

        This is the ASPX page code that I used:
        Code:
        <div style="width:50%; margin:0 auto;">
          <asp:Repeater ID="rptAuditActionsInformation" runat="server">
            <HeaderTemplate>
                <pagertemplate></pagertemplate>
                <asp:LinkButton ID="Header_Page_First" runat="server" CommandArgument="First" CommandName="Page" Text="First Page" ToolTip="First Page" /> |
                <asp:LinkButton ID="Header_Page_Prev" runat="server" CommandArgument="Prev" CommandName="Page" Text="Back" ToolTip="Back" /> | 
                <asp:LinkButton ID="Header_Page_Next" runat="server" CommandArgument="Next" CommandName="Page" Text="Forward" ToolTip="Forward" /> | 
                <asp:LinkButton ID="Header_Page_Last" runat="server" CommandArgument="Last" CommandName="Page" Text="Last Page" ToolTip="Last Page" /> |
                <asp:LinkButton ID="Header_PrintAllLinkButton" runat="server" CommandName="PrintAll" Text="Print" ToolTip="Print" Width="20px" />
            </HeaderTemplate>
            <ItemTemplate>
                <div style="margin:2px; border-bottom:solid 1px black">
                    <div>
                        <asp:Localize ID="IDPrompt" runat="server" Text="ID: "> </asp:Localize>
                        <asp:Localize ID="ID" runat="server" Text='<%# Eval("ID") %>' ></asp:Localize>
                    </div>
                    <div>
                        <asp:Localize ID="NamePrompt" runat="server" Text="Name: "> </asp:Localize>
                        <asp:Localize ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Localize>
                    </div>
                </div>
            </ItemTemplate>
            <FooterTemplate>
                <pagerstyle cssclass="footer" font-italic="True" />
                <pagertemplate></pagertemplate>
                <asp:LinkButton ID="Page_First" runat="server" CommandArgument="First" CommandName="Page" Text="First Page" ToolTip="First Page" /> |
                <asp:LinkButton ID="Page_Prev" runat="server" CommandArgument="Prev" CommandName="Page" Text="Back" ToolTip="Back" /> | 
                <asp:LinkButton ID="Page_Next" runat="server" CommandArgument="Next" CommandName="Page" Text="Forward" ToolTip="Forward" /> | 
                <asp:LinkButton ID="Page_Last" runat="server" CommandArgument="Last" CommandName="Page" Text="Last Page" ToolTip="Last Page" /> |
                <asp:LinkButton ID="PrintAllLinkButton" runat="server" CommandName="PrintAll" Text="Print" ToolTip="Print" Width="20px" />
            </FooterTemplate>
          </asp:Repeater>
        </div>


        This is the VB.NET code for the page:
        Code:
        Private pagedData As PagedDataSource 'Aids with paging the data displayed in the Repeater
        Private characters As List(Of DisneyCharacter) 'Used as the data source displayed in the Repeater
        
        'Names of Disney Characters used to create the data source for the Repeater
        Private characterNames() As String = {"Mickey Mouse", "Mini Mouse", "Pluto", "Goofy", "Donald Duck", "Daisy Duck", "Chip", "Dale", _
                                    "Abby Mallard", "Abigail Gabble", "Abis Mal", "Abu", "Adella", "The Agent", "Agent Wendy Pleakley", "Akela", "Al the Alligator", "Aladar", "Aladdin", "Alameda Slim ", "Alan-a-Dale", _
                                    "Alana", "Jennifer Hale", "Alcmene", "Alice", "Amelia Gabble", "Amos Slade", "Amphitryon", "Anastasia Tremaine", "Anda", "Andrina", _
                                    "Cadpig", "Calliope Lillias", "Captain Amelia", "Captain Gantu", "Captain James Hook", "Carl the Robot", "Carlotta the Maid", "Cassim"}
        
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Initializing data source for the Repeater
            InitializeRepeaterDataSource()
        End Sub
        
        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            'Setting the data source for the Repeater and binding it to the data
            rptAuditActionsInformation.DataSource = pagedData
            rptAuditActionsInformation.DataBind()
        End Sub
        
        Private Sub rptAuditActionsInformation_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptAuditActionsInformation.ItemCommand
        
            'Handling the paging action that the user performed using the controls in the footer.
            Select Case e.CommandArgument
                Case "First"
                    pagedData.CurrentPageIndex = 0
                Case "Prev"
                    If pagedData.CurrentPageIndex > 0 Then
                        pagedData.CurrentPageIndex = pagedData.CurrentPageIndex - 1
                    End If
                Case "Next"
                    If pagedData.CurrentPageIndex < pagedData.PageCount - 1 Then
                        pagedData.CurrentPageIndex = pagedData.CurrentPageIndex + 1
                    End If
                Case "Last"
                    pagedData.CurrentPageIndex = pagedData.PageCount - 1
            End Select
        
            'Storing the current page index in ViewState so that we can retrieve 
            'this information the next time the page is loaded to set the PagedDataSource control
            ViewState("currentPage") = pagedData.CurrentPageIndex
        End Sub
        
        Private Sub InitializeRepeaterDataSource()
        
            'Retrieving the list of characters from Session
            characters = CType(Session("characters"), List(Of DisneyCharacter))
        
            'If the list doesn't exist in Session, creating it and storing it in Session for next time
            If characters Is Nothing Then
                characters = New List(Of DisneyCharacter)
                For i As Integer = 0 To characterNames.Length - 1
                    characters.Add(New DisneyCharacter(i * 111, characterNames(i)))
                Next
                Session("characters") = characters
            End If
        
            'Initializing the PagedDataSource which aids with paging the data in the Repeater
            pagedData = New PagedDataSource
        
            'Setting the data source for the PagedDataSource to the data
            pagedData.DataSource = characters
        
            'Setting the number of items to display in a page
            pagedData.PageSize = 5
        
            'Specifying that the control should allow paging to take place
            pagedData.AllowPaging = True
        
            'Setting the current page index to that which is stored in ViewState (if there is nothing in ViewState then it is 0)
            pagedData.CurrentPageIndex = CType(ViewState("currentPage"), Integer)
        End Sub
        
        
        ''' <summary>
        ''' This class is used as data for the repeater
        ''' </summary>
        ''' <remarks></remarks>
        Private Class DisneyCharacter
            Private _id As Integer
            Private _name As String
            Public Property ID As Integer
                Get
                    Return _id
                End Get
                Set(ByVal value As Integer)
                    _id = value
                End Set
            End Property
            Public Property Name As String
                Get
                    Return _name
                End Get
                Set(ByVal value As String)
                    _name = value
                End Set
            End Property
            Public Sub New(ByVal id As Integer, ByVal name As String)
                Me.ID = id
                Me.Name = name
            End Sub
        End Class
        Everything works fine....give it a try if you want :)

        -Frinny
        Last edited by Frinavale; Oct 15 '10, 04:28 PM.

        Comment

        • guideX

          #5
          Very simple solution, take the code outside of the repeater's footer, and use it outside of the repeater entirely. In fact, if you have an event to use, like a button click, simply never use it in the footer, problem solved.

          Comment

          Working...