I'm using Telerik RadGrid to present some objects to user. When the grid is filled thru NeedDataSource event, everything works fine. But I'm experimenting with asynchronous data loading and binding data to grid thru RadGrid.Datasource seems better for this.
My problem is that when I bind data this way - setting RadGrid.Datasource and calling RadGrid.DataBind(), than the DropDown list for choosing PageSize stops showing the selected pagesize. The dropdown list alone works fine, I can change to different number of items per page, but no matter what i do, the number is not visible.
Sorting and filtering of the data is done in WCF service, so I have set AllowCustomPaging to true and I set the VirtualItemCount just after setting the DataSource property (just before calling DataBind()).
The grid then looks like this:
RadGrid
5 Answers, 1 is accepted
The following online example shows the steps on how to implement simple data-binding with paging. You need to bind the grid on page load and in the PageIndexChanged event handler as shown below:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/simplebinding/defaultcs.aspx
Greetings,
Pavlina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Hi Pavlina,
Thank you for your answer, but unfortunately, this won't solve my problem. As I wrote before, I'm trying to load data for the grid asynchronously, using PageAsyncTask and page.RegisterAsyncTask. This way I have the data for grid available sometime between Page_PreRender and Page_PreRenderComplete. Problem is that when I try to bind the data in Page_PreRenderComplete (when it's sure I have them available) then I end up with InvalidOperationException - "Script controls may not be registered after PreRender". It doesn't matter whether I use RadGridList.DataSource or bind them thru NeedDataSource event and RadGrid.Rebind.
Only work-around I found is binding the data in the moment I have them available - which is after PreRender but still before PreRenderComplete. This way I can bind them to the grid, without experiencing the exception (as is seen on the image I posted before).
Only problem which remains (and that is why I started this thread) is that the DropDown text which should present the number of items per page stays empty.
I tried now to implement the same (binding the data when they arrive, before the Page_PreRenderComplete event) through the NeedDataSource event and it works for me too, but does the same thing with the DropDown text.
I will probably attach some source code to explain this better.
Yours sincerely,
Jiri Koudelka
This is some "explaining" class to show how I have implemented the binding of the grid:
using
System;
using
System.Collections.Generic;
using
System.Web;
using
System.Web.UI;
using
Telerik.Web.UI;
namespace
BCMS.Web.Forms.Tests
{
public
class
TelerikTest : System.Web.UI.UserControl
{
private
class
DataObject
{
public
string
Value {
get
;
set
; }
}
private
class
ResponseClass
{
public
List<DataObject> Data {
get
;
set
; }
public
int
TotalCount {
get
;
set
; }
}
private
delegate
ResponseClass AsyncMethodCaller();
private
AsyncMethodCaller asyncMethodCaller;
protected
RadGrid RadGridList;
private
ResponseClass asyncResponse;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(IsPostBack ==
false
)
{
//Add task for async loading of data
GetGridData();
}
}
protected
void
Page_PreRender(
object
sender, EventArgs e)
{
//The async loading of data did not started yet
}
protected
void
Page_PreRenderComplete(
object
sender, EventArgs e)
{
//now the data are sure to be loaded, but binding the RadGrid throws
//InvalidOperationException - "Script controls may not be registered after PreRender".
}
protected
void
RadGridList_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
if
(asyncResponse !=
null
)
{
RadGridList.DataSource = asyncResponse.Data;
RadGridList.VirtualItemCount = asyncResponse.TotalCount;
}
}
private
void
GetGridData()
{
//Creates PageAsyncTask for loading the data asynchronously
PageAsyncTask task =
new
PageAsyncTask(
new
BeginEventHandler(BeginAsyncMethod),
new
EndEventHandler(EndAsyncMethod),
new
EndEventHandler(TimeOut),
null
,
true
);
this
.Page.RegisterAsyncTask(task);
}
private
IAsyncResult BeginAsyncMethod(
object
sender, EventArgs e, AsyncCallback callback,
object
state)
{
//Starts the data-loading method through asynchronous delegate
asyncMethodCaller =
new
AsyncMethodCaller(AsyncMethod);
return
asyncMethodCaller.BeginInvoke(callback, state);
}
private
ResponseClass AsyncMethod()
{
//This is where the data are loaded
var response =
new
ResponseClass()
{
Data =
new
List<DataObject>(),
TotalCount = 100,
};
return
response;
}
private
void
EndAsyncMethod(IAsyncResult asyncResult)
{
//Sets AsyncMethod's response to property and rebinds the grid
asyncResponse = asyncMethodCaller.EndInvoke(asyncResult);
RadGridList.Rebind();
//This can be implemented another way as well:
//asyncResponse = asyncMethodCaller.EndInvoke(asyncResult);
//RadGridList.DataSource = asyncResponse.Data;
//RadGridList.VirtualItemCount = asyncResponse.TotalCount;
//RadGridList.DataBind();
}
private
void
TimeOut(IAsyncResult asyncResult)
{
asyncResponse =
null
;
}
}
}
Can you please open a formal support ticket and send a runnable version of your project for us to debug locally and make the needed changes, so that it behaves as expected.
Other than this it is hard to determine the source of the problem based on the provided code.
Regards,
Pavlina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.