Dynamic Web Part Loading Error: "path" argument cannot be empty if the "type" argument is UserControl

I would like to dynamically load web parts, but without using LoadControl*. However, when I try to use the following code, a runtime error is thrown whose meaning is mostly meaningless to me.

Control ctrl = (Control)new MyUserControl();

// Control ctrl = LoadControl( "MyUserControl.ascx" );

ctrl.ID = "DynamicUserControl";

GenericWebPart myWebPart = MyWebPartManager.CreateWebPart(ctrl);

MyWebPartManager.AddWebPart( myWebPart, MyWebPartManager.Zones[1], 0 );

 

The error is thrown on the last line, during AddWebPart:

 

Additional information: The "path" argument cannot be empty if the "type" argument is UserControl.

 

Can anyone explain what might be causing this error, and what exactly it means? Note, the line I've commented out is how web parts are typically loaded dynamically, but as I said, I'd prefer to avoid using LoadControl if possible.

 

Thanks,

 

- Travis

 

 

* LoadControl() looks to be comparatively slow when profiled, and I would rather provide controls through, say, a factory pattern instead.

0
dasein_fiasco
7/3/2006 4:32:30 PM
asp.net.personalization 3779 articles. 0 followers. Follow

3 Replies
789 Views

Similar Articles

[PageSpeed] 30

Please add the code for the entire page.

Not enough information.

0
TheLionClaws
7/3/2006 7:27:44 PM

Thanks for the reply. It appears that you can place the code anywhere in the page lifecycle and it throws the same error regardless; I didn't see how the location impacts the problem (how to instantiate usercontrols for CreateWebPart()), so I just included the snippet. For the sake of argument, just assume that it's going in the Page_Load().

protected void Page_Load( object sender, EventArgs e )

{

Control ctrl = (Control)new MyUserControl();

ctrl.ID = "DynamicUserControl";

GenericWebPart myWebPart = MyWebPartManager.CreateWebPart(ctrl);

MyWebPartManager.AddWebPart( myWebPart, MyWebPartManager.Zones[1], 0 );

}

The control itself isn't anything special. I'll create a dummy one for completeness' sake.

    public partial class MyUserControl: System.Web.UI.UserControl, IWebPart

    {

        protected void Page_Load( object sender, EventArgs e )

        {

        }

 

        protected string _title = "Blah";

        public string Title

        {

            get { return _title; }

            set { _title = value; }

        }

 

        protected string _subtitle = "";

        public string Subtitle

        {

            get { return _subtitle; }

            set { _subtitle = value; }

        }

 

        protected string _titleUrl = "";

        public string TitleUrl

        {

            get { return _titleUrl; }

            set { _titleUrl = value; }

        }

 

        protected string _description = "";

        public string Description

        {

            get { return _description; }

            set { _description = value; }

        }

 

        private string _titleIconImageUrl = "";

        public string TitleIconImageUrl

        {

            get { return _titleIconImageUrl; }

            set { _titleIconImageUrl = value; }

        }

 

        private string _catalogIconImageUrl = "";

        public string CatalogIconImageUrl

        {

            get { return _catalogIconImageUrl; }

            set { _catalogIconImageUrl = value; }

        }

 

    }

0
dasein_fiasco
7/3/2006 7:52:10 PM
When you add a dynamic WebPart, the WebParts framework needs to have enough information so it can recreate the WebPart on future requests.  For custom controls, this is just the type name.  For user controls, a path to the user control is required.  Without the path, the WebParts framework would not be able to recreate the WebPart on future requests.

My recommendation would be to use just LoadControl().  I doubt it would be the perf bottleneck in your app.

-Mike

http://blogs.msdn.com/mharder

This posting is provided "AS IS" with no warranties, and confers no rights.
0
mharder
7/13/2006 5:41:16 PM
Reply:

Web resources about - Dynamic Web Part Loading Error: "path" argument cannot be empty if the "type" argument is UserControl - asp.net.personalization

Resources last updated: 11/27/2015 7:05:53 AM