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?