This is a migrated thread and some comments may be shown as answers.

showColumn/hideColumn cause errors on postback

2 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Means
Top achievements
Rank 1
Eric Means asked on 07 Jun 2010, 05:20 PM

I have a situation where depending on other controls on the page, I want to hide or show two different columns in a RadGrid. Unfortunately, I'm having trouble getting this to work.

The grid is being bound client-side to an ASP.NET AJAX web service. When the grid is being databound (i.e. in its ClientEvents-OnDataBinding event or in its ClientEvents-OnDataBound event [I've tried both, same results]) I use a loop like the following to hide or show the appropriate column: 

    var view = g_grdProjects.get_masterTableView();  
    for (var i = 0; i < view.get_columns().length; ++i)  
    {  
        if (view.get_columns()[i].get_uniqueName() == "Project.ProjectStageDate")  
        {  
            if (projectType == 'Closed')  
                view.showColumn(i);  
            else 
                view.hideColumn(i);  
        }  
        if (view.get_columns()[i].get_uniqueName() == "Project.Stage")  
        {  
            if (projectType == 'Closed')  
                view.hideColumn(i);  
            else 
                view.showColumn(i);  
        }  
    }  
 
 

This works as expected; however, when I use this code, posting back the page (which is required at some points) results in the following error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Web.UI.RadGrid.LoadClientState(Dictionary`2 clientState) +4232
   Telerik.Web.UI.RadCompositeDataBoundControl.LoadPostData(String postDataKey, NameValueCollection postCollection) +149
   Telerik.Web.UI.RadCompositeDataBoundControl.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +13
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +8905944
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +878

 
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

I've tried replacing the calls to showColumn and hideColumn with calls to the column's set_visible() method instead; that resolves the postback issue but results in incorrect behavior (the columns hide fine, but calling set_visible(true) does not show the columns, so they remain hidden forever).

Is there a way to use showColumn()/hideColumn() and keep the ability to post back when necessary?

2 Answers, 1 is accepted

Sort by
0
Eric Means
Top achievements
Rank 1
answered on 10 Jun 2010, 09:25 PM
The problem appears, per this thread, to be that we have ViewState disabled. I'll implement the suggested workaround.
0
Yavor
Telerik team
answered on 11 Jun 2010, 07:10 AM
Hi Eric,

If you have disabled ViewState, you can either enable it, or use the suggested workaround. I hope one of these options is suitable for you.

Kind regards,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Eric Means
Top achievements
Rank 1
Answers by
Eric Means
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or