Any time came across a scenario where you have to pass property value from WebPart hosting the UserControl to pass value in SharePoint 2007.
Here is a small example how you can achive this.
The Solution structure
As you see by default you can't see the User Control template ,the easiest way of doing this Unload the project "Right Click" click as shown in the image
Add this GUID under the section where it says
<ProjectTypeGuids>
{349C5851-65DF-11DA-9384-00065B846F21};
Save it and reload the project "Bingo" you can see the User Control template.
WebPart Code :
public class ValueToUC : System.Web.UI.WebControls.WebParts.WebPart
{
private bool _error = false;
private string _myName = null;
private const string _ascxPath = @"~/_CONTROLTEMPLATES/ValueToUC/ValueUC.ascx";
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category("My Properties")]
[WebDisplayName("MyName")]
[WebDescription("MyName")]
public string MyProperty
{
get { return _myName; }
set { _myName = value; }
}
public ValueToUC()
{
this.ExportMode = WebPartExportMode.All;
this.ChromeType = PartChromeType.None;
}
protected override void CreateChildControls()
{
if (!_error)
{
try
{
base.CreateChildControls();
ValueUC uc = (ValueUC)Page.LoadControl(_ascxPath);
uc.parentWp = this;
this.Controls.Add(uc);
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
private void HandleException(Exception ex)
{
this._error = true;
this.Controls.Clear();
this.Controls.Add(new LiteralControl(ex.Message));
}
}
User Control Code:
public partial class ValueUC : System.Web.UI.UserControl
{
public ValueToUC parentWp
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = parentWp.MyProperty;
}
}
As you observe I am using the WebPart class as a property which will help in getting the value to the User Control.