日期:2014-05-18  浏览次数:20381 次

动态加载用户控件,如何设置控件属性?
<uc7:NewsList   CategoryID= "2 "   TitleLength= "25 "   id= "NewsList1 "   runat= "server "/>
然后这样:
UserControl   uc   =   (UserControl)LoadControl( "Modules/NewsList.ascx ");
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(uc);
加载是加载了,但是属性值怎么弄?

------解决方案--------------------
UserControl uc = (UserControl)LoadControl( "Modules/NewsList.ascx ");


uc.TitleLength= 25;


PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(uc);
------解决方案--------------------
楼上好快
------解决方案--------------------
说了NewsList是你NewsList.ascx的类名,我这里是猜测的,具体是什么要看你的。

因为你必须强制转化成NewsList类才行。

我估计可能你要这样才行

Modules.NewsList uc = (Modules.NewsList)LoadControl( "Modules/NewsList.ascx ");

具体看你的NewsList.ascx的类名和命名空间
------解决方案--------------------
MyUserControl uc = LoadControl( "Modules/NewsList.ascx ") as MyUserControl;
uc.MyProperty = ....