RadGrid - Values not retained after Postback
The RadGrid is generated from the server-side with template fields. NeedDataSource() is what is used for data binding. On every postback, RadGrid loses the values as the NeedDataSource is not called.Manually doing a Rebind() on the Onload() does not help either.
The Grid Structure is defined in OnLoad() and executed once on !IsPostback()
I remember reading somewhere that when building the Grid structure dynamically from the serverside, the grid 'needs' to be Rebind() on every postback.
Does RadGrid not maintain the values on postback? Is this somet开发者_开发技巧hing to do with ViewState?
The "NeedDataSource" event should occur on postbacks as well. You could try setting up your page using an RadAjaxManager I suppose. Does your grid load data on the first load? Try following the demo pages they have set up on the grids, as its one of the best examples of the different ways to set them up.
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/simplebinding/defaultcs.aspx
As per radgrid documentation if you create the grid programmatically you need to create it in OnInit method: http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html (Creating a RadGrid on Page_Init section) The onLoad can be used when the grid is partially added into the markup. Hope this helps.
create function to bind data to Radgrid
private void BindData()
{
//Get data from database
//DBData can be datatable or list
RadGrid1.DataSource = DBData;
}
then call in page load event
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
BindData();
}
}
catch (Exception ex)
{
}
}
then you can use same function in NeedDataSource event
protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
BindCommentData();
}
精彩评论