This is a migrated thread and some comments may be shown as answers.

Using ItemCommand - Paging/Sorting non-functional

2 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin Wood
Top achievements
Rank 1
Kevin Wood asked on 06 Jun 2010, 03:23 AM
I think this must be a simple thing, and I have searched the forums - but apparently I have completely missed it.

I have a simple RadGrid bound to an ObjectDataSource - the automatic paging / sorting work just fine.

            <telerik:RadGrid ID="rgrdUsers" runat="server" Skin="Web20" AutoGenerateColumns="False" 
                DataSourceID="odsUsers" GridLines="None" AllowSorting="True" AllowPaging="True"
                <PagerStyle AlwaysVisible="True" /> 
                <MasterTableView DataSourceID="odsUsers" DataKeyNames="UserID" PagerStyle-AlwaysVisible="True"
                    <Columns> 
                        <telerik:GridTemplateColumn UniqueName="ViewProfile" ItemStyle-HorizontalAlign="Center" 
                            HeaderStyle-HorizontalAlign="Center"
                            <ItemTemplate> 
                                <asp:ImageButton ID="lnkViewProfile" CommandName="ViewProfile" runat="server" ToolTip="View Profile" 
                                    ImageUrl="~/images/icon_hostusers_16px.gif"></asp:ImageButton> 
                            </ItemTemplate> 
                            <HeaderStyle HorizontalAlign="Center" Font-Bold="true" /> 
                            <ItemStyle HorizontalAlign="Center" /> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn UniqueName="ViewAdItems" ItemStyle-HorizontalAlign="Center" 
                            HeaderStyle-HorizontalAlign="Center"
                            <ItemTemplate> 
                                <asp:ImageButton ID="lnkViewAdItems" CommandName="ViewAdItems" runat="server" ToolTip="View Ad Items" 
                                    ImageUrl="~/images/file.gif"></asp:ImageButton> 
                            </ItemTemplate> 
                            <HeaderStyle HorizontalAlign="Center" Font-Bold="true" /> 
                            <ItemStyle HorizontalAlign="Center" /> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridBoundColumn DataField="Username" HeaderText="Username" SortExpression="Username" 
                            UniqueName="Username"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" 
                            UniqueName="FirstName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" SortExpression="LastName" 
                            UniqueName="LastName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" 
                            UniqueName="Telephone"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="CreatedOnDate" DataType="System.DateTime" HeaderText="Created" 
                            SortExpression="CreatedOnDate" UniqueName="CreatedOnDate" DataFormatString="{0: M/d/yy h:mm tt}"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="LastModifiedOnDate" DataType="System.DateTime" 
                            HeaderText="LastActive" SortExpression="LastModifiedOnDate" UniqueName="LastModifiedOnDate" 
                            DataFormatString="{0: M/d/yy h:mm tt}"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="ExpiryDate" DataType="System.DateTime" HeaderText="Expires" 
                            SortExpression="ExpiryDate" UniqueName="ExpiryDate" DataFormatString="{0: M/d/yy h:mm tt}"
                        </telerik:GridBoundColumn> 
                        <telerik:GridTemplateColumn HeaderText="Props/Ads" SortExpression="CountAdItems" 
                            UniqueName="CountAdItems" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                            <ItemTemplate> 
                                <asp:Label ID="lblProps" runat="server" Text='<% #Eval("CountAdItems") %>'></asp:Label> 
                                / 
                                <asp:Label ID="lblAds" runat="server" Text='<% #Eval("CountAdInstances") %>'></asp:Label> 
                            </ItemTemplate> 
                            <HeaderStyle HorizontalAlign="Center" Font-Bold="true" /> 
                            <ItemStyle HorizontalAlign="Center" /> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn HeaderText="Views/Clicks" SortExpression="CountAdViews" 
                            UniqueName="CountAdViews" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
                            <ItemTemplate> 
                                <asp:Label ID="lblViews" runat="server" Text='<% #Eval("CountAdViews") %>'></asp:Label> 
                                / 
                                <asp:Label ID="lblClicks" runat="server" Text='<% #Eval("CountAdClicks") %>'></asp:Label> 
                            </ItemTemplate> 
                            <HeaderStyle HorizontalAlign="Center" Font-Bold="true" /> 
                            <ItemStyle HorizontalAlign="Center" /> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridBoundColumn DataField="AffiliateName" HeaderText="Affiliate" SortExpression="AffiliateName" 
                            UniqueName="AffiliateName"
                        </telerik:GridBoundColumn> 
                    </Columns> 
                    <PagerStyle AlwaysVisible="True"></PagerStyle> 
                </MasterTableView> 
            </telerik:RadGrid> 

When I add the following ItemCommand code, the actual ItemCommands do work as expected - however, paging and sorting is no longer functional. I see the paging/sorting post back (via firebug) - and what I think is happening is the paging/sorting fire the ItemCommand code, but there is nothing there to handle that. So my question is, what needs to be done to keep the automatic paging/sorting in place, along with the following ItemCommand logic?

   Protected Sub rgrdUsers_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgrdUsers.ItemCommand 
 
            Dim UserID As Integer = Int32.Parse(e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("UserID")) 
 
            Select Case e.CommandName 
 
                Case "ViewProfile" 
                    Response.Redirect("~/MyProfile.aspx?UserID=" & UserID) 
                Case "ViewAdItems" 
                    Response.Redirect("~/Home.aspx?UserID=" & UserID) 
                
            End Select 
        End Sub 


Thanks - Kevin

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 08 Jun 2010, 05:03 PM
Hello Kevin,

Please note that when you sort your grid using the column headers, the Item referenced through the GridCommandEventArgs (the "e" parameter of the ItemCommand event handler) is GridGroupHeaderItem. Since this item is not a GridDataItem its ItemIndex is -1, and trying to get DataKeyValues[-1] should cause an "Index was out of range" exception. The same scenario is valid if you "page" your grid - although the item type in this case is GridPagerItem, it also has an ItemIndex of -1.

To workaround this issue, I would suggest that you first check whether the e.Item object is of type GridDataItem and execute your code only in case the check passes.

I hope this helps,
Martin
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Kevin Wood
Top achievements
Rank 1
answered on 15 Jun 2010, 01:25 PM
Thanks - that worked - inserting the following condition in the code fixed my issue!

            If TypeOf (e.Item) Is Telerik.Web.UI.GridDataItem Then



Tags
Grid
Asked by
Kevin Wood
Top achievements
Rank 1
Answers by
Martin
Telerik team
Kevin Wood
Top achievements
Rank 1
Share this question
or