Can anyone help me with Paging and Radgrid? I've tried looking at the tutorials on the Telerik site but they don't have any real world examples.
I've got my own objects and I don't use an ObjectDataSource...I'm trying to get it to work with my own code. This is what I'm trying to do. On page load, I get my list of Products and set it in Viewstate and then I bind it to my Grid. I then use the RadGrid_PageIndexChanged even to go through my Product List from ViewState and find the next set of products within my collection and re-bind my grid.
Would this work? Here's what I got:
<telerik:RadGrid ID="gridProducts" runat="server" AllowPaging="True" AutoGenerateColumns="False" GridLines="None" Skin="Outlook" width="675px" ShowFooter="True"> <HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Outlook"></HeaderContextMenu> <PagerStyle AlwaysVisible="True" /> <MasterTableView allowcustompaging="True" PageSize="5"> <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings> <RowIndicatorColumn> <HeaderStyle Width="20px"></HeaderStyle> </RowIndicatorColumn> <ExpandCollapseColumn> <HeaderStyle Width="20px"></HeaderStyle> </ExpandCollapseColumn> <Columns> <telerik:GridTemplateColumn HeaderText="Item" UniqueName="Item"> <ItemTemplate > <telerik:RadBinaryImage ID="rbiItem" runat="server" /><br /> <asp:LinkButton ID="lnkView" runat="server">View Details</asp:LinkButton> </ItemTemplate> <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" Width="125px" /> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Item Description" UniqueName="ItemDescription"> <ItemTemplate> <asp:Label ID="lblItem" CssClass="ItemName" runat="server" Text=""></asp:Label> <br /> <asp:Label ID="lblDescription" runat="server" Text=""></asp:Label> </ItemTemplate> <ItemStyle VerticalAlign="Top" /> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Price" UniqueName="Price"> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text=""></asp:Label> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Right" Width="50px" /> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Buy Now"> <ItemTemplate> <asp:ImageButton ID="imgCart" OnCommand="imgCart_Command" CommandName="Add" runat="server" /> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" Width="100px" /> </telerik:GridTemplateColumn> </Columns> <EditFormSettings> <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn> </EditFormSettings> <PagerStyle AlwaysVisible="True" /> </MasterTableView> <ClientSettings> <Selecting AllowRowSelect="True" /> </ClientSettings> <FilterMenu EnableImageSprites="False"></FilterMenu> </telerik:RadGrid>...and here's the code behind:
Partial Public Class Products Inherits BasePage Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then InitializeControls() End If End Sub Private Sub InitializeControls() '== Labels lblTitle.Text = "Products" btnSearch.Text = "Search" imgBtnSearch.ImageUrl = "~/images/go.bmp" imgBtnSearch.Width = Unit.Pixel(30) imgBtnSearch.Height = Unit.Pixel(30) lblNoRecords.Visible = False lblNoRecords.Text = "There are no items to display." radCategories.DataTextField = "CategoryName" radCategories.DataValueField = "CategoryID" radCategories.DataSource = CategoryBL.GetAllCategories() radCategories.DataBind() radCategories.Items.Insert(0, New RadComboBoxItem("select a category", "")) radCategories.Items.Insert(1, New RadComboBoxItem("View All", "-1")) LoadGrid() End Sub Private Sub LoadGrid() 'Set the products in ViewState Me.Products = ProductBL.GetActiveProducts gridProducts.DataSource = Me.Products gridProducts.DataBind() End Sub Private Sub gridProducts_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridProducts.ItemDataBound If (TypeOf (e.Item) Is GridDataItem) Then Dim dr As PawnShop.Entities.Product = CType(e.Item.DataItem, PawnShop.Entities.Product) Dim lnkView As LinkButton = CType(e.Item.FindControl("lnkView"), LinkButton) lnkView.PostBackUrl = "Details.aspx?id=" & HttpUtility.HtmlEncode(dr.ProductID.ToString) Dim lblItem As Label = CType(e.Item.FindControl("lblItem"), Label) lblItem.Text = HttpUtility.HtmlEncode(dr.Name) Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label) lblDescription.Text = HttpUtility.HtmlEncode(dr.Description) Dim lblPrice As Label = CType(e.Item.FindControl("lblPrice"), Label) lblPrice.Text = String.Format(Constants.CURRENCY, dr.Price) Dim imgCart As ImageButton = CType(e.Item.FindControl("imgCart"), ImageButton) 'imgCart.ImageUrl = "~/images/shoping_cart_sm.png" imgCart.ImageUrl = "~/images/buy_now.gif" imgCart.CommandArgument = dr.ProductID.ToString imgCart.CommandName = "Add" Dim rbiItem As RadBinaryImage = CType(e.Item.FindControl("rbiItem"), RadBinaryImage) rbiItem.DataValue = dr.imgContent rbiItem.Width = 100 'rbiItem.Height = 100 rbiItem.ResizeMode = BinaryImageResizeMode.Fit rbiItem.AutoAdjustImageControlSize = False rbiItem.AlternateText = HttpUtility.HtmlEncode(dr.Name) 'Dim imgProduct As Image = CType(e.Item.FindControl("imgProduct"), Image) 'imgProduct.ImageUrl = "~/Handler.ashx?id=" & dr.ProductID End If End Sub Private Sub gridProducts_PageIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridPageChangedEventArgs) Handles gridProducts.PageIndexChanged Dim startRowIndex As Integer = gridProducts.CurrentPageIndex * gridProducts.PageSize Dim maximumRows As Integer = gridProducts.PageSize Dim myList As New List(Of Product) For i As Integer = 0 To Me.Products.Count If i >= gridProducts.CurrentPageIndex AndAlso i <= gridProducts.PageSize Then myList.Add(Me.Products(i)) End If Next If myList.Count > 0 Then gridProducts.DataSource = myList End If End Sub Private Sub gridProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridProducts.SelectedIndexChanged Dim startRowIndex As Integer = gridProducts.CurrentPageIndex * gridProducts.PageSize Dim maximumRows As Integer = gridProducts.PageSize Dim myList As New List(Of Product) For i As Integer = 0 To Me.Products.Count If i >= gridProducts.CurrentPageIndex AndAlso i <= gridProducts.PageSize Then myList.Add(Me.Products(i)) End If Next If myList.Count > 0 Then gridProducts.DataSource = myList End If End Sub Private Property Products() As List(Of Product) Get Return CType(ViewState("Products"), List(Of Product)) End Get Set(ByVal value As List(Of Product)) ViewState("Products") = value End Set End Property End ClassThe paging at the bottom of my grid only shows 1 page but I have more products to display. Am I going about this the wrong way? Any help on this would be appreciated.
Thanks