当前位置: 代码迷 >> ASP.NET >> 动态加载用户控件,怎么设置控件属性
  详细解决方案

动态加载用户控件,怎么设置控件属性

热度:9188   发布时间:2013-02-25 00:00:00.0
动态加载用户控件,如何设置控件属性?
<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 = ....
  相关解决方案