动态给文本框赋值 和 找页面控件的方法
2010-11-28 18:47
363 查看
动态的给文本框赋值的方法:
string[] searchstring = model.SEARCH_STRING.Split(' ');
int searchcount = searchstring.Length;//得到长度如:2
for (int i = 1; i < searchcount; i++)//动态的给文本框赋值的方法
{
XXXXX
}
//这样的最好的地方在于 不用写太多的判断 判断有多少文本框
//下面的方法是找控件的方法:
来自:本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/net20/20101125/9631.html
asp.net母版页使用this.FindControl()方法的问题,在没有使用MasterPage之前,页面this访问到的对象就是他自己,所以使用this,FindControl也就没什么问题。
但是使用MasterPage之后,this.Control.Count=1,就只有一个。跟踪一下: this.Controls[0] {ASP.module_mp1_master} System.Web.UI.Control {ASP.module_mp1_master} .
也就是说,加入masterPage之后他的控件的顺序就变了。(以前看过一个在asp.net1.1下的MasterPage实现,也是类似的方式,重新组织控件)。这就是问题所在。
解决方法:
把原来这样的使用:
protected void AssignControls()
{
this.lblError = this.FindControl("lblError") as Label;
this.dgItemList = this.FindControl("dgItemList") as DataGrid;
}
变成这样调用:
protected void AssignControls()
{
this.lblError = this.Master.FindControl("Content").FindControl("lblError") as Label;
this.dgItemList = this.Master.FindControl("Content").FindControl("dgItemList") as DataGrid;
}
也就是说,先使用this.Master.FindControl("Content")来找到在MasterPage上相对的位置,然后再按照以前的调用方式就可以了。
或者使用这个函数
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
string[] searchstring = model.SEARCH_STRING.Split(' ');
int searchcount = searchstring.Length;//得到长度如:2
for (int i = 1; i < searchcount; i++)//动态的给文本框赋值的方法
{
XXXXX
}
//这样的最好的地方在于 不用写太多的判断 判断有多少文本框
//下面的方法是找控件的方法:
来自:本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/net20/20101125/9631.html
asp.net母版页使用this.FindControl()方法的问题,在没有使用MasterPage之前,页面this访问到的对象就是他自己,所以使用this,FindControl也就没什么问题。
但是使用MasterPage之后,this.Control.Count=1,就只有一个。跟踪一下: this.Controls[0] {ASP.module_mp1_master} System.Web.UI.Control {ASP.module_mp1_master} .
也就是说,加入masterPage之后他的控件的顺序就变了。(以前看过一个在asp.net1.1下的MasterPage实现,也是类似的方式,重新组织控件)。这就是问题所在。
解决方法:
把原来这样的使用:
protected void AssignControls()
{
this.lblError = this.FindControl("lblError") as Label;
this.dgItemList = this.FindControl("dgItemList") as DataGrid;
}
变成这样调用:
protected void AssignControls()
{
this.lblError = this.Master.FindControl("Content").FindControl("lblError") as Label;
this.dgItemList = this.Master.FindControl("Content").FindControl("dgItemList") as DataGrid;
}
也就是说,先使用this.Master.FindControl("Content")来找到在MasterPage上相对的位置,然后再按照以前的调用方式就可以了。
或者使用这个函数
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
相关文章推荐
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法
- 用反射方法使用户控件动态调用父页面的方法
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法
- 子页面赋值给父页面的控件方法
- 向页面动态载入用户控件和自定义控件的方法不同
- 用户控件动态调用父页面的方法
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法
- 用反射方法使用户控件动态调用父页面的方法
- asp.net简单页面控件赋值实现方法
- 用反射方法使用户控件动态调用父页面的方法
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- 接上一篇,在cs文件中往页面动态添加meta标签时出现“控件包含代码块(即 ),因此无法修改控件集合”错误的解决方法
- 子页面赋值给父页面的控件方法
- web页面中表单控件(如checkbox,select,radio等)的赋值回写的方法介绍
- 用反射方法使用户控件动态调用父页面的方法
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法