Telerik Grid在ASP.NET WebForms中回发后不显示数据

时间:2016-10-07 18:00:28

标签: c# asp.net telerik

我正在尝试使用一些基本数据创建Telerik RadGrid控件,并且正在使用here提供的Simple Data Bind方法。

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

这是背后的代码。当用户单击“运行”按钮时,一切正常。但是,如果我单击网格中的任何事件,例如在列上排序,我会看到页面刷新,但网格不显示任何内容。

非常感谢任何建议。

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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您使用RadGrid,则希望利用 NeedDataSource 事件。

主要原因是它知道何时绑定数据。您甚至不需要在代码中使用 RadGrid1_SortCommand

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;
}