ASP.NET 2.0 Portal Web Parts - AddWebPart

M

mrsmitty

Okay can't figure out how to get this to work? Works with a standard
TextBox but not my user control? Help is much appreciated. Thanks.

UserControl googleSearch = new UserControl();
googleSearch.LoadControl("GoogleSearch.ascx");
googleSearch.ID = "UserControlGoogleSearch";
WebPart googleWebPart = WebPartManager1.CreateWebPart(googleSearch);
WebPartManager1.AddWebPart(googleWebPart, WebPartZone2, 1);

Exception thrown on last line:

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

at
System.Web.UI.WebControls.WebParts.WebPartManager.IsAuthorized(Type
type, String path, String authorizationFilter, Boolean isShared)
at
System.Web.UI.WebControls.WebParts.WebPartManager.IsAuthorized(WebPart
webPart)
at
System.Web.UI.WebControls.WebParts.WebPartManager.AddDynamicWebPartToZone(WebPart
webPart, WebPartZoneBase zone, Int32 zoneIndex)
at
System.Web.UI.WebControls.WebParts.WebPartManager.AddWebPart(WebPart
webPart, WebPartZoneBase zone, Int32 zoneIndex)
at _Default.AddWebPart(Object sender, EventArgs e) in
c:\app_dev\SulzerPortal\Default.aspx.cs:line 66
 
M

mrsmitty

I finally was able to resolve this issue. I still think it is a bug and
do not completely understand why one method works and the other
doessn't. Here's the scoop.

UserControl googleSearch = new UserControl();
MODIFIED: googleSearch = (UserControl)LoadControl("GoogleSearch.ascx");
googleSearch.ID = "UserControlGoogleSearch";
WebPart googleWebPart = WebPartManager1.CreateWebPart(googleSearch);
WebPartManager1.AddWebPart(googleWebPart, WebPartZone2, 1);

Instead of calling googleSearch.LoadControl(path), I used the page
LoadControl(path). The page load control seems to maintain the path
argument that was originally passed to it. When you call AddWebPart the
part must be authorized which needs that path argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top