LoadControl() with FormView NullReferenceException

I just discovered a little tip with dynamic control creation via the ASP.NET LoadControl() method that might be helpful.

LoadControl() has two signatures:

  • LoadControl(string)
  • LoadControl(System.Type, object[])

I was trying to use the 2nd version and pass along a parameter object to the constructor of my user control.  This user control contained a FormView.  Everything compiled, but I kept getting a runtime NullReferenceException telling me that my FormView was NULL.  Odd, since I basically cut and pasted the control from a different control that was known to work.  The only difference was that this time I was dynamically loading the control.

I switched to using the first version of LoadControl, passing in a virtual path to the control, and everything worked fine.

I can’t say exactly why, but something about the Render chain is different between the two versions.


0 Responses to “LoadControl() with FormView NullReferenceException”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

TwitterCounter for @anthonyrstevens
Add to Technorati Favorites

RSS Feed

View Anthony Stevens's profile on LinkedIn

%d bloggers like this: