C# – Why is the Telerik RadGrid disappearing on PostBack

asp.netc++data-bindingpostbacktelerik

The Telerik DLL version for our project is 2012.3.1016.40

We are using a Telerik RadGrid in one of our ASP.NET pages. We bind the Telerik RadGrid to an ASP.NET DataTable Object.
We also have an ASP.NET Button that Closes the page if it gets clicked.

We invoke REST-based Web Service Methods, and populate an ASP.NET DataTable Object with the data returned.

We bind the ASP.NET DataTable Object to a Telerik RadGrid.

The ASP.NET DataTable Object is populated using OnNeedDataSource method.

protected void commentRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
        try
        {

             // The ViewStateThreadedDiscussion has a Type of ASP.NET DataTable.
             // Blah Blah Code That Populates the ViewStateThreadedDiscussion DataTable Blah Blah

             commentRadGrid.DataSource = new string[] { };

            if (ViewStateThreadedDiscussion == null)
            {
                // Extremely Important to use empty string double quotes if
                // the threadedDiscussionWithinDataTable DataTable is null because
                // Telerik only works properly if you assign the Telerik RadGrid DataSource
                // to an empty string with double quotes when we have a DataTable variable that is null.
                // Never assign null to the Telerik RadGrid DataSource because Telerik DLL throws Error.

               commentRadGrid.DataSource = "";
            }
            else
            {
                commentRadGrid.DataSource = ViewStateThreadedDiscussion;
            }



        } // end of try
        catch (Exception ex)
        {
            log4NetInstance.Error(ex.Message);
            log4NetInstance.Error(ex.StackTrace);
            log4NetInstance.Error(ex.ToString());
        } // end of catch (Exception ex)
    } // end of protected void SharedWithRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

I have a Telerik RadButton with an OnClick method specified. Within the Telerik RadButton OnClick method, I invoke a Rebind on the Telerik RadGrid. Here is the Telerik RadButton OnClick Method code:

 protected void OlderCommentsButton_Click(object sender, EventArgs e)
    {
        try
        {

            commentRadGrid.Rebind();
        }
        catch (Exception ex)
        {
            log4NetInstance.Error(ex.Message);
            log4NetInstance.Error(ex.StackTrace);
            log4NetInstance.Error(ex.ToString());
        } // end of catch (Exception ex)
    } // end of protected void OlderCommentsButton_Click(object sender, EventArgs e)

Here is my declaration and configuration for Telerik RadGrid:

<telerik:RadGrid runat="server" Width="60%" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  HeaderStyle-Width="60%" ID="commentRadGrid" AllowFilteringByColumn="true" AutoGenerateColumns="false"
    AllowPaging="true" OnPageIndexChanged="commentRadGrid_PageIndexChanged"  OnNeedDataSource="commentRadGrid_NeedDataSource" PageSize="100" Skin="Default" AllowSorting="true" AutoPostBack="true" ShowStatusBar="true" AllowCustomPaging="True"
    GridLines="none">

I have Visual Studio 2012, and I run the application in debug mode with break points. I used the "Add Watch" feature provided by Visual Studio 2012 on various variables like the ViewStateThreadedDiscussion ASP.NET DataTable, and the Telerik RadGrid DataSource property. Everything seems to populated with the proper values even on Post Back.

However, when the ASPX page loads on PostBack the
Telerik RadGrid disappears.

Why is the Telerik RadGrid disappearing on PostBack?

Best Solution

You should not call commentRadGrid.DataBind() inside commentRadGrid_NeedDataSource event.

Advanced Data-binding (using NeedDataSource event)

You should never call the Rebind() method in a NeedDataSource event handler.You should never call DataBind() as well when using advanced data binding through NeedDataSource. For Microsoft GridView-like data binding, see Simple Data-binding.

Update:

If you set AllowCustomPaging="True", you will need to provider VirtualItemCount.

It basically means you are giving RadGrid to maximum 100 rows (PageSize="100") although you have a million records. Therefore, RadGrid need to know how many total records you have in order to display pagination.

Note: If you have only few records, you want to provide all records to RadGrid. If so, you do not need both AllowCustomPaging="True" and VirtualItemCount.

Here is the sample example -

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadGrid runat="server" 
    ID="commentRadGrid" 
    AllowFilteringByColumn="true" 
    AutoGenerateColumns="True"
    AllowPaging="true" 
    OnPageIndexChanged="commentRadGrid_PageIndexChanged" 
    OnNeedDataSource="commentRadGrid_NeedDataSource" 
    PageSize="100" Skin="Default" 
    AllowSorting="true" 
    ShowStatusBar="true" 
    AllowCustomPaging="True"
    GridLines="none">
</telerik:RadGrid>
<asp:Button runat="server" ID="OlderCommentsButton" 
    OnCommand="OlderCommentsButton_Click" Text="Post Back" />

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

protected void commentRadGrid_NeedDataSource(
    object sender, GridNeedDataSourceEventArgs e)
{
    var users = new List<User>
    {
        new User {FirstName = "John", LastName = "Doe"},
        new User {FirstName = "Jenny", LastName = "Doe"},
    };

    commentRadGrid.DataSource = users;
    commentRadGrid.MasterTableView.VirtualItemCount = users.Count;
}

protected void OlderCommentsButton_Click(object sender, EventArgs e)
{
    try
    {
        commentRadGrid.Rebind();
    }
    catch (Exception ex)
    {
    }
}

protected void commentRadGrid_PageIndexChanged(
    object sender, GridPageChangedEventArgs e)
{

}