2016-10-07 61 views
0

我想用一些基本數據創建一個Telerik RadGrid控件,並使用提供的簡單數據綁定方法hereTelerik Grid在ASP.NET WebForms回發後沒有顯示數據

<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

嘗試做你的IsPostBack檢查,並在頁面加載方法結合或改變數據表是靜態 – MethodMan

+0

也許你的問題是'postback'。檢查這篇文章http://stackoverflow.com/questions/34544500/get-text-value-from-textbox-after-value-text-changed-server-side –

+0

@MethodMan,如何將我的DataTable更改爲靜態幫助? – Pavel

回答

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

我試過了這似乎並不起作用,因爲在第一次加載頁面時,用戶還沒有指定BeginDate和EndDate,然後單擊「運行」按鈕。所以這些值是NULL。 – Pavel

+0

你可以在PageLoad上隱藏radGrid;它將不會調用NeedDataSource,直到您點擊BtnRun按鈕。我更新了答案。 – Win