ASP.NET 2.0 Portal Web Parts - AddWebPart

  • Thread starter Thread starter mrsmitty
  • Start date Start date
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
 
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.
 
Back
Top