Рецепт передачи параметров веб-части в ascx, если ascx не компилируется в dll
????????? ???????????????? ??????. ?? ?????? ???????????????? ??????? ?????????? (ascx) ??????? ????? ? ??? ? ????? ...\Template\CONTROLTEMPLATES ? ?? ????????????? ? ??????, ? ????????????? "?? ????". ?????? ???? ????? ?????? ??????? ???-?????, ??????? ???????? ? ???? ???? ??????? ?????????? ? ???????? ??? ??????????? ?????????.
? ???? ????????? ????????? ?????????:
public
class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart{
private int pageSize = 25;
[WebBrowsable(
true),
Personalizable(PersonalizationScope.Shared),
DefaultValue(
"25"),
Category(
" "),
FriendlyNameAttribute(
"- "),
XmlElement(ElementName =
"PageSize")]
public int PageSize{get{return pageSize;}set{pageSize = value;}}
protected override void OnInit(EventArgs e){
base.OnInit(e);
this.ExportMode = WebPartExportMode.All;
UserControl ctrl = LoadControl("~/_controltemplates/TestUserControl.ascx", new object[1] { PageSize });
if (ctrl != null){this.Controls.Add(ctrl);}
}
private UserControl LoadControl(string UserControlPath, params object[] constructorParameters){
List<Type> constParamTypes = new List<Type>();
foreach (object constParam in constructorParameters) constParamTypes.Add(constParam.GetType());
UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;
ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());
if (constructor == null){throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());}else{constructor.Invoke(ctl, constructorParameters);}
return ctl;
}
}