Based on the stack trace, the InvalidOperationException
is thrown during LoadControlState
, which is called before view state has been loaded. If you don't set PagedControlID
declaratively (in the .ascx), then it will still be an empty string at this point in the page life cycle.
The MSDN Library example differs from your code in one important respect: the DataPager
is being dynamically constructed, initialized, and added to the page. LoadControlState
doesn't get called until the DataPager
is added to the page, at which point PagedControlID
has already been initialized.
It doesn't appear that the ASP.NET team really intended to support the PagedControlID
property being set dynamically. I suppose the only thing you can try is to emulate the MSDN Library example and add the DataPager
dynamically.