Pavel
Pavel

Reputation: 766

Telerik Grid not showing data after postback in ASP.NET WebForms

I am trying to create a Telerik RadGrid control with some basic data, and am using the Simple Data Bind method provided here.

<div id="divRadGrid">
    <telerik:RadGrid ID="RadGrid1" runat="server" OnSortCommand="RadGrid1_SortCommand" AllowSorting="True"></telerik:RadGrid>
</div>

Here's the code behind. When the user clicks the Run button, everything works fine. However, if I click on any event inside the grid, such as Sorting on a column, I see the page refreshes, but the grid does not display anything.

Any suggestions would be very much appreciated.

using System;
using System.Data;
using System.Collections.Generic;
using Telerik.Web.UI;

namespace TelerikDemoWebAppBlank2
{
    public partial class Default : System.Web.UI.Page
    {
        public DataTable SalespersonProductionDT { get; set; }
        public DataTable NewBusinessDT { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnRun_Click(object sender, EventArgs e)
        {
            getData();
            processNewBusiness();
        }

        protected void RadGrid1_SortCommand(object sender, GridSortCommandEventArgs e)
        {
            dataBindGrid(RadGrid1, NewBusinessDT);
        }

        private void getData()
        {
            SalespersonProductionDT = new DataTable();
            Sprocs._pDash_SalespersonProduction(BeginDate.SelectedDate.Value, EndDate.SelectedDate.Value, SalespersonProductionDT);

            NewBusinessDT = new DataTable();
            Sprocs._pDash_NewBusiness(BeginDate.SelectedDate.Value, EndDate.SelectedDate.Value, NewBusinessDT);
        }

        private void processNewBusiness()
        {
            dataBindChart(RadHtmlChart3, NewBusinessDT);
            dataBindGrid(RadGrid1, NewBusinessDT);
        }

        private void dataBindChart(RadHtmlChart radHtmlChart, DataTable dt)
        {
            radHtmlChart.DataSource = dt;
            radHtmlChart.DataBind();
        }

        private void dataBindGrid(RadGrid radGrid, DataTable dt)
        {
            radGrid.DataSource = dt;
            radGrid.DataBind();
        }
    }
}

Upvotes: 0

Views: 1944

Answers (1)

Win
Win

Reputation: 62260

If you use RadGrid, you want to take the advantage of NeedDataSource event.

The main reason is it knows when to bind data. You don't even need RadGrid1_SortCommand in your code.

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
       radGrid.Visible = false;
   }
}

protected void BtnRun_Click(object sender, EventArgs e)
{
    radGrid.Visible = true;
    radGrid.Rebind();
}

// Make sure you attach RadGrid_NeedDataSource event to grid inside markup.
protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
   var dt = new DataTable();
   Sprocs._pDash_NewBusiness(BeginDate.SelectedDate.Value, 
      EndDate.SelectedDate.Value, dt);

   radGrid.DataSource = dt;
}

Upvotes: 1

Related Questions