RadControls for ASP.NET AJAX
Typically RadGrid stores in ViewState only items/controls collection, not datasource data. Sometimes though the page ViewState can grow too big and might significantly increase the page download time.
You can control this behavior by setting the EnableViewState property to false if you do not wish the data for the controls in the grid to to be persisted in the ViewState.
This means that the control will need to be rebound on every request: either by firing the NeedDataSource event or by going through an ASP.NET 2.x/3.x data source control.
Although Simple DataBinding is not supported when ViewState is disabled, there is a work-around that could be used in some cases. You need to call DataBind()
method on the Grid instance on Page_Load event when the Page has not been postback to the server and on Page_Init event if the Page has been postback.
CopyASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
EnableViewState="false" GridLines="None">
</telerik:RadGrid>
<asp:Button Text="Postback" ID="Button1" runat="server" />
CopyC#
public partial class Default_Cs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadGrid1.DataSource = MasterData();
RadGrid1.DataBind();
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
RadGrid1.DataSource = MasterData();
RadGrid1.DataBind();
}
}
public List<MasterItem> MasterData()
{
List<MasterItem> masterItems = new List<MasterItem>();
for (int i = 0; i < 6; i++)
{
masterItems.Add(new MasterItem(i, "Master_Value_" + i.ToString()));
}
return masterItems;
}
}
public class MasterItem
{
public int MasterItemID { get; set; }
public string MasterItemValue { get; set; }
public MasterItem(int masterItemID, string masterItemValue)
{
MasterItemID = masterItemID;
MasterItemValue = masterItemValue;
}
}
CopyVB.NET
Partial Class Default_VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
RadGrid1.DataSource = MasterData()
RadGrid1.DataBind()
End If
End Sub
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
If Page.IsPostBack Then
RadGrid1.DataSource = MasterData()
RadGrid1.DataBind()
End If
End Sub
Public Function MasterData() As List(Of MasterItem)
Dim masterItems As New List(Of MasterItem)()
For i As Integer = 0 To 5
masterItems.Add(New MasterItem(i, "Master_Value_" + i.ToString()))
Next
Return masterItems
End Function
End Class
Public Class MasterItem
Public Property MasterItemID() As Integer
Get
Return m_MasterItemID
End Get
Set(value As Integer)
m_MasterItemID = Value
End Set
End Property
Private m_MasterItemID As Integer
Public Property MasterItemValue() As String
Get
Return m_MasterItemValue
End Get
Set(value As String)
m_MasterItemValue = Value
End Set
End Property
Private m_MasterItemValue As String
Public Sub New(masterItemID__1 As Integer, masterItemValue__2 As String)
MasterItemID = masterItemID__1
MasterItemValue = masterItemValue__2
End Sub
End Class
When having EnableViewState set to false, RadGrid will use the Page control state to store only the absolutely necessary bits of data that preserve the current paging, sorting, and grouping state.
Some operations in Telerik RadGrid like data extraction through the ExtractValuesFromItem
method, grouping, hierarchical views expand/collapse, automatic data source operations, custom edit forms (WebUserControl and FormTemplate)
or filtering require that the EnableViewState is set to true.
If no data is persisted for items in Telerik RadGrid (EnableViewState=false), then the state of items is lost after postback. Generally, data-source
persistence optimization should be used if the small size/speed of a Page, showing RadGrid, is crucial for the application. If ViewState optimization is enabled,
RadGrid will fire NeedDataSource and will bind after each post back to restore its items. RadGrid and its TableView manage
the state of the following features while the EnableViewState property is set to False:
Indexes of selected items (Telerik RadGrid support this automatically only for server-side selection)
Indexes of edited items
Group-by expressions and settings (but not the expanded state of the grouped items)
Sort expressions
Style properties (but not if any style is applied on a single cell or row in ItemDataBound event)
Columns order and other column properties
All settings concerning hierarchy structure (but not the expanded state of the items)
RadGrid does NOT manage the state for the following features when its EnableViewState property is set to false:
If you would like to retain the expanded state of items or server-side selection with ViewState disabled, you may consider using the approach depicted in
this code library entry
(applicable for explicit rebinds or ViewState switched off) or this knowledge base article
. An alternative solution would be to turn on the ViewState of the grid and optimize its performance by using RadCompression and its page adapters as explained in this article.
See Also