Using WebControls in templates instead of WebParts
March 1st, 2006 // 5:03 pm @ Amar
This is a small tip which I am not sure if many people are aware. When you try to add a WebPart to certain locations in your site definition files, sometimes the page will crash and refuse to load. If you place a webcontrol instead, then it works.
I generally use the starter webpart template and change it to inherit from a WebControl instead. I also create a wrapper webpart, which wraps my webcontrol, hence giving me flexibility to use the webpart or webcontrol interchangebly. You will get access to full SharePoint context even from a WebControl.
The following piece of code depicts the wrapper webpart for MyWebControl.
The WebControl Code:
namespace MyCustomCode
{
public class MyWebControl : System.Web.UI.WebControls
{
// WebControl code goes here
}
}
The WebPart Code:
namespace MyCustomCode
{
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private MyWebControl webControl;
protected override void RenderWebPart(HtmlTextWriter output)
{
this.webControl.RenderControl(output);
}
protected override void CreateChildControls()
{
webControl = new MyWebControl();
this.Controls.Add(webControl);
}
}
}
Edited: Formatted code for line breaks.
Category : SharePoint
http://
4 years ago
Dear Amar:
I am trying to modify site template definition file “default.aspx” to add a web part,
I tried everything, using web part, web control, and the tip you mentioned in your site “Using Web controls in template instead of web part”.
but I get error as such:
A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe.
it only works if I import the web part through “Modify Shared page –> Add Web Part” on the specific site default.aspx. But always get the error if I manually type in the web part or control in to the site definition default.aspx.
I registed safe controls, put in the GAC…did everything, but it only works for specific site default.aspx, but on site template definition default.aspx.
Do you have any suggestion on this? could you send me the code on how you put the webpart or web control into site definition default.aspx?
Thank you for your help!
Hope to hear from you soon!
Laura
Amar Galla
4 years ago
Hi Laura,
The web.config would be the one corresponding to the IIS Web root which your site / portal is pointing. So if you installed your portal on c:\inetpub\wwwroot then you need to edit the web.config in that wwwroot folder. If you have a wss site created elsewhere, to get your webpart working on your default.aspx, edit the web.config corresponding to that directory.
Hope this helps.
Amar