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

.请问:动态加载控件Page.LoadControl 怎么对定位到加载的控件的id
动态加载一个用户控件test1.ascx
里面有个     <TD> <INPUT   id=TextBox2   name=TextBox2>   </TD>
<TD> <INPUT   id=Button1   onclick=modify(this)   type=button   value=修改>   </TD>    
===============================================================
a.aspx动态加载到PlaceHolder       DynamicControl  
加载代码:
string   strVisulPath   =   "TemplateControl\\justtest.ascx ";
                DynamicControl.Controls.Add(Page.LoadControl(strVisulPath));
===============================================================
a.aspx.cs
请问里面怎么对TextBox2赋值?

------解决方案--------------------
不是runat=server的?
------解决方案--------------------
Control ctl = Page.LoadControl(strVisulPath);
ctl.id= "TextBox2 "; //赋id值
DynamicControl.Controls.Add(ctl);

然后你要使用这个控件的时候先查找这个控件:

Control ctl = DynamicControl.FindControl( "TextBox2 ");



------解决方案--------------------
HtmlInputButton hib = DynamicControl.FindControl( "Button1 ") as HtmlInputButton;
Response.Write(hib.Value);

会不会使ID的问题呢?

用户控件的Id要有一个$隔开的前缀
------解决方案--------------------
用户控件最好加个public 属性.

如:

public TextBox TextBox1
{
get
{
return this.TextBox2;
}
}

//然后就可在外访问了.