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

[Solved] RadGrid - Paging Doesn't Work When Building Grid Dynamically

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JD
Top achievements
Rank 1
JD asked on 31 Jul 2013, 09:43 PM
Hi,
I'm new to Telerik.  Virtually all of the pages I work with are actually user controls, being built entirely dynamically, usually in OnInit.  I'm finding it hard to get the Grid (first control I've tried) to behave in this situation.  Any client actions performed don't seem to produce the intended result.  For instance, Paging just stays on Page 1, Page Size does not take, etc.

You'll notice I'm using the Ajax Panel, but I've tried it without, as well.  Thought it might have to do with ViewState/Postback and you can see commented code related to this, but all the permutations I've tried produce the same behavior.

Most of this code was copied and pasted from Telerik demos, primarily the sample at http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section3.

Thanks in advance for your feedback.

JD

   protected void _radGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            //if (Page.IsPostBack)
            //    return;
 
            string connString = "trust me, the connection string works;";
 
            _connection = new SqlConnection();
            _connection.ConnectionString = connString; // connectionString.ConnectionString;
            _connection.Open();
 
            using (SqlCommand command = _connection.CreateCommand())
            {
 
                command.CommandText = "dbo.Adddress_ListByCountry";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@CountryCode", "FRA");
 
                SqlDataReader reader = command.ExecuteReader();
 
                ((RadGrid)source).DataSource = reader;
            }
             
 
        }
 
        protected void _radGrid1_DataBound(object sender, EventArgs e)
        {
            if (_connection.State == ConnectionState.Open)
            {
                _connection.Close();
            }
        }
 
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
 
            _radGrid1 = DefineGridStructure();
 
            _radAjaxLoadingPanel = new RadAjaxLoadingPanel();
            _radAjaxLoadingPanel.ID = "_radAjaxLoadingPanel";
 
            _radAjaxManager = new RadAjaxManager();
            _radAjaxManager.ID = "_radAjaxManager";
            _radAjaxManager.DefaultLoadingPanelID = "_radAjaxLoadingPanel";
 
            this.Controls.Add(_radAjaxManager);
            this.Controls.Add(_radAjaxLoadingPanel);
 
            AjaxSetting setting = new AjaxSetting();
            AjaxUpdatedControl target = new AjaxUpdatedControl(_radGrid1.ID, _radAjaxLoadingPanel.ID);
            setting.UpdatedControls.Add(target);
            setting.AjaxControlID = _radGrid1.ID;
            _radAjaxManager.AjaxSettings.Add(setting);
 
            _gridPanel = new Panel();
            _gridPanel.ID = "_gridPanel";
 
            _gridPanel.Controls.Add(_radGrid1);
            this.Controls.Add(_gridPanel);
 
             
 
        }
 
        private RadGrid DefineGridStructure()
        {
            RadGrid radGrid1 = new RadGrid();
            radGrid1.Skin = "Default";
            radGrid1.Width = Unit.Percentage(100);
            radGrid1.PageSize = 15;
            radGrid1.AllowPaging = true;
            radGrid1.AutoGenerateColumns = false;
            radGrid1.ID = "_radGrid1";
            radGrid1.DataBound += _radGrid1_DataBound;
            radGrid1.NeedDataSource += _radGrid1_NeedDataSource;
            radGrid1.AllowPaging = true;
            //radGrid1.EnableViewState = true;
 
            //Add columns
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "AddressId";
            boundColumn.HeaderText = "Address ID";
            radGrid1.MasterTableView.Columns.Add(boundColumn);
 
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "AddressText";
            boundColumn.HeaderText = "Address";
            radGrid1.MasterTableView.Columns.Add(boundColumn);
 
            return radGrid1;
        }

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 05 Aug 2013, 12:33 PM
Hello,

Your code seems OK. Could you try to remove these lines of the code:

_gridPanel = new Panel();
_gridPanel.ID = "_gridPanel";
_gridPanel.Controls.Add(_radGrid1);
this.Controls.Add(_gridPanel);

and add the Grid directly to the Controls collection of the user control:

this.Controls.Add(_radGrid1);

Additionally, are you calling DataBind() method of RadGrid anywhere in your project? Could you share your full page source code along with the code-behind file content?

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
JD
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or