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

Grid resets to default data bind on paging

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shane
Top achievements
Rank 1
Shane asked on 29 Jul 2011, 07:29 PM
Hello,

I have a RadGrid that upon page load is populated with data via a default query (built via the GUI) thats fine... I also have drop down list on that page and when a user select a category from that list the Grid Rebinds to only show items in that category, It works fine (hand coded)... However when a user then selects the next page the RadGrid reloads items from the default query...

How to get the RadGrid to use the last Rebind/query when paging?

(ASP/VB.Net 2008)

Thanks
Shane

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Jul 2011, 08:41 PM
hi,

please check below code and let me know if any concern.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Updategridfromddl.aspx.cs"
    Inherits="TelerikTest.Web.Updategridfromddl" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager" runat="server">
    </telerik:RadScriptManager>
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="--Select--" Value="0" Selected="True"></asp:ListItem>
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
        </asp:DropDownList>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server"  AllowPaging="true" PageSize="3"
            onneeddatasource="RadGrid1_NeedDataSource"></telerik:RadGrid>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace TelerikTest.Web
{
    public partial class Updategridfromddl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadGrid1.Rebind();
        }
 
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            List<TwoStringParameterBE> lstTwoStringParameterBE = getList();
 
            if (Convert.ToInt32(DropDownList1.SelectedValue) > 0)
            {
                lstTwoStringParameterBE = lstTwoStringParameterBE.FindAll(delegate(TwoStringParameterBE obj) { return obj.Name == DropDownList1.SelectedItem.ToString(); });
            }
 
            RadGrid1.DataSource = lstTwoStringParameterBE;
        }
 
        protected List<TwoStringParameterBE> getList()
        {
            List<TwoStringParameterBE> lstTwoStringParameterBE = new List<TwoStringParameterBE>();
            for (int i = 0; i < 10; i++)
            {
                TwoStringParameterBE obj = new TwoStringParameterBE();
                obj.Name = "1";
                obj.value = "1";
                lstTwoStringParameterBE.Add(obj);
            }
            for (int i = 0; i < 5; i++)
            {
                TwoStringParameterBE obj = new TwoStringParameterBE();
                obj.Name = "2";
                obj.value = "2";
                lstTwoStringParameterBE.Add(obj);
            }
            return lstTwoStringParameterBE;
        }
    }
    public class TwoStringParameterBE
    {
        private string _stringName;
        private string _stringValue;
 
        public string Name
        {
            get
            {
                return this._stringName;
            }
            set
            {
                this._stringName = value;
            }
        }
 
        public string value
        {
            get
            {
                return this._stringValue;
            }
            set
            {
                this._stringValue = value;
            }
        }
    }
 
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Shane
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or