» 网友学堂 » NET教程 » 掉掉注意了,如何动态加载用户控件(ascx) -> 当日更新
掉掉注意了,如何动态加载用户控件(ascx)
作者:问天 发表时间:2007-2-20 11:29 阅读:325次 在百度搜索相关内容

注: 本方法是我同事李强原创

步骤一: 把ascx控件拖入设计窗口,然后转入html模式,保留定义
如果你需要多个ascx的话,请全部拖入。
但是把相应的内容删除。(因为我们需要动态加载)
在需要填入控件的地方,放个容器,比如td,并设定在服务器端运行

<TD id="tdpan" runat=server></TD>

2:动态调用
UserControl myusercontrol = (UserControl) LoadControl ("../includes/pageNavigater.ascx") ;
Type myusertype = myusercontrol.GetType();
//下面是给ascx赋值
PropertyInfo myuserinfo1 = myusertype.GetProperty("RelatedDatagrid");//) .GetProperty("RelatedDatagrid");
myuserinfo1.SetValue(myusercontrol,gridhwcy ,null);


PropertyInfo mypassinfo = myusertype.GetProperty("torefresh");
[color=#FFFFFF'][/color]

mypassinfo.SetValue(myusercontrol,true,null );

PropertyInfo myuserdatasource = myusertype.GetProperty("RelatedDataSource");
myuserdatasource.SetValue(myusercontrol,dv,null);

tdpan.Controls.Clear();
tdpan.Controls.Add(myusercontrol);

#Advertisement