Monday, October 23, 2006

NullReferenceException in GridView BuildCallbackArgument when trying to render it dynamically with paging

Whoa! Big title there. This article applies to errors in ASP.NET 2.0 when trying to dynamically render a gridview with paging.

I have been trying to "Ajaxify" my web programming and I've found that the easiest method is to wrap everything I need in Web User Controls, render the controls, then send the rendered string through ajax to fill some innerHTML.
Well, the fine people at Microsoft thought otherwise. I have been trying to render a web user control with a gridview inside it for 3 hours now and nothing helped. I kept getting a NullReferenceException when calling GridView.DataBind and the StackTrace showed it originated in the BuildCallBackArgument(int) method.
Nothing helped except actually decompiling the code of the GridView itself. I've found out that it called a method in the Page of the control, in other words it needed a page. I gave it a page, but then another problem occured that I'd already solved here. I already had a parent page that overrode the offending method, so the final code is this:
ParentPage pp=new ParentPage();  // where ParentPage is a Page with VerifyRenderingInServerForm(Control control) overriden so it doesn't do anything
pp.EnableEventValidation = false; //another silly error
uc.Page = pp; //uc is the user control needing rendering


Manik said...

Hey Man!! thanks a lot !!
I had this problem too .. got it working just fine!!
Don't know Why Microsoft people couldn't mention this in their documentation .. do they expect everyone to read their code .. esp. wen they won't even let it out..

Mike said...

I have this exact same frustrating problem, but I don't understand your solution.

1) What is the ParentPage? I don't see that type available.

2) What is "bs" in your code example?

Siderite said...

Oops, sorry. I often copy paste then addapt the code directly in the blog.
I've fixed the entry, 'bs' was 'pp' and added an explanation for ParentPage. It's a custom object inherited from Page which overrides VerifyRenderingInServerForm(Control control) with a method with an empty body.

Anonymous said...

Really thank you for this code !

I managed to get it working after 3 long hours of search.

naga said...

hi, I too have the same problem can u pls explain your code. what is the parent page in your code?

Siderite said...

It's really simple. You create a new class called ParentPage, inheriting from System.Web.UI.Page.In it you override VerifyRenderingInServerForm and in it you do nothing, not even call the base.VerifyRenderingInServerForm method.

Then you use an instance of the ParentPage class as the Page property of the grid you want to render.

naga said...

thank u. I have one problem in exporting the gridview to an excel sheet. in the grid im using the paging but in the excel sheet i want all the rows to be displayed.

Siderite said...

Just set AllowPaging to false when you render it to export.

naga said...

hey thanks for your fast response. I tried that too but its not working. still its showing the current page data only. suggest some other way.