Dynamic RadPageView loses content on postback
1 Answer 13 Views
Rafael
Top achievements
Rank 1
Rafael asked on 04 May 2021, 08:59 PM | edited on 04 May 2021, 09:07 PM

I'm creating a RadGrid dynamically and inserting a RadGrid into it.

When I change the RadGrid page, it disappears. How to keep the RadPageView content through Postback?

Besides that, none of RadGrid events are fired when the page changes.

My code:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageViewProblem.aspx.cs" Inherits="WebApp.PageViewProblem" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <telerik:RadTabStrip runat="server" ID="radTabStrip1" MultiPageID="radMultiViewSearchResult"></telerik:RadTabStrip>
            <telerik:RadMultiPage runat="server" ID="radMultiView1" SelectedIndex="0" EnableViewState="true"></telerik:RadMultiPage>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace WebApp
{
    public partial class PageViewProblem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            RadTab tab1 = new Telerik.Web.UI.RadTab("Tab1");
            radTabStrip1.Tabs.Add(tab1);
            RadPageView resultPageView1 = new RadPageView();
            resultPageView1.ID = "radPageView1";
            radMultiView1.PageViews.Add(resultPageView1);

            RadGrid grid1 = new RadGrid();
            grid1.ID = "RadGrid1";
            grid1.PageSize = 5;
            grid1.AllowPaging = true;
            grid1.AllowCustomPaging = true;
            grid1.PagerStyle.AlwaysVisible = true;
            grid1.VirtualItemCount = 50;
            grid1.NeedDataSource += (sender, args) =>
            {
                grid1.DataSource = SimulateDataSource(grid1);
            };
            grid1.PageIndexChanged += (sender , args) =>
            {
                Debug.WriteLine("Please, load page number: {grid1.currentPage}");
            };
            resultPageView1.Controls.Add(grid1);
        }

        private object SimulateDataSource(RadGrid grid)
        {
            List<ItemData> result = new List<ItemData>();
            for (int i = grid.CurrentPageIndex + 1; i <= grid.CurrentPageIndex + 5; i++)
            {
                result.Add(new ItemData(i, $"Item Description {i}"));
            }
            return result;
        }
        private class ItemData
        {
            public int Value { get; set; }
            public string Description { get; set; }
            public ItemData(int value, string description)
            {
                this.Value = value;
                this.Description = description;
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 07 May 2021, 02:41 PM

Hello Rafael,

Due to the nature of the ASP.NET Framework, it is important to use the correct events when creating controls dynamically. Check out the Page Life Cycle In ASP.NET article to learn why the events are important.

This applies for Telerik and non-Telerik ASP.NET WebForms controls. 

Try to move the code from the Page_Load to Page_Init (without the "if(!IsPostBack)" condition) and see if that will fix the issue.

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Rafael
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or