Webpart references usercontrol in two ways:
1. Reference quickpart
2. Develop a webpart to expose a reference address attribute. The Code is as follows:
View code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
namespace WebPartCollection
{
[Guid("1c7e7f5f-c4d6-4628-8785-3804bc0f5c85")]
public class UserControlWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public UserControlWebPart()
{
}
private string _ControlPath;
[Personalizable]
[WebBrowsable]
[WebDisplayName("Control File Path")]
public string ControlPath
{
get { return _ControlPath; }
set { _ControlPath = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (string.IsNullOrEmpty(_ControlPath))
return ;
try
{
Control calendar = Page.LoadControl(_ControlPath);
if (calendar!=null)
{
this.Controls.Add(calendar);
}
}
catch (Exception ex)
{
this.Controls.Add(new LiteralControl(ex.Message));
}
}
}
}
The controlpath address format is as follows :~ /_ Controltemplates/calendar. ascx. From the above address format, we can see that the calendar. ascx file is placed in the 12 \ template \ controltemplates folder.