日期:2014-05-19  浏览次数:21039 次

动态加载自定义控件的问题
c#中,我如何动态加载控件,并传递参数。

比如我的自定义控件为:
"~/Controls/WebUserControl.ascx ";
我在控件里的接受参数为:string     str.
我怎样动态调用自定义控件,并且传参数。
Control     ctrl=   (UserControl)LoadControl( "~/Controls/WebUserControl.ascx ");
ctrl.str= "字符串 "   //这行好像不行


------解决方案--------------------
我也不知道 学习

------解决方案--------------------
在WebUserControl.ascx中添加一个属性,比方说:
privte string str;

public string Str
{
get{return str;}
set{str = value};
}

到aspx文件中就可以使用了
ascx控件ID.Str = "字符串 ";
------解决方案--------------------
Control ctrl= (UserControl)LoadControl( "~/Controls/WebUserControl.ascx ");
ctrl.str= "字符串 ";
改成
UserControl ctrl= (UserControl)LoadControl( "~/Controls/WebUserControl.ascx ");
ctrl.str= "字符串 ";
试一下
------解决方案--------------------
强制转换一下
UserControl ctrl= (UserControl)LoadControl( "~/Controls/WebUserControl.ascx ");
------解决方案--------------------
ctrl是Control类型的
没有str整个变量,需要转换下。