Monday, March 28, 2011

Pass Web part Custom property value to UserControls

// Create Custom Property in Webpart ---------------------------
[
Personalizable(),
Category("Miscellaneous"),
DefaultValue(""),
WebBrowsable(true),
WebDisplayName("Enter SK Form ID"),
WebDescription("Enter SK Form ID")
]
public string SKForm
{
get { return _skform; }
set { _skform = value; }
}
// End of Create Custom Property in Webpart ---------------------------

//-------------------------------------------------------------
protected override void CreateChildControls()
{
base.CreateChildControls();

userControl = (UserControl)Page.LoadControl(@"/_controltemplates/PraveshUserControl.ascx"); // Call UC

Type myControlType = userControl.GetType();
PropertyInfo MyControlProperty = myControlType.GetProperty("FormGUIDNos");
MyControlProperty.SetValue(userControl, SKForm, null); // Set value to UC Public Property
this.Controls.Add(userControl);
}

// End of -------------------- Create Child Controls

//-----------------------User Controls---------

public string _guidno;

public string FormGUIDNos
{
get { return _guidno; }
set { _guidno = value; }
}
// and Access this property anywhere in UC

No comments:

Post a Comment