我定义了一个类,但使用时总是出错。
#region 得到页面中所有指定类型的Control
/// <summary>
/// 使用完方法后可从这个对象得到返回的控件集合
/// </summary>
public ArrayList AlControls;
/// <summary>
/// 得到页面中所有指定类型的Control
/// </summary>
/// <param name="pagecontrols">使用形如this.Controls返回的对象</param>
public void ReturnControls(System.Web.UI.ControlCollection pagecontrols, string stringControlType )
{
if (!string.IsNullOrEmpty(stringControlType)&&pagecontrols.Count>0)
{
foreach(Control ct in pagecontrols)
{
if (ct.GetType().ToString() == stringControlType)
{
this.AlControls.Add(ct);
}
else
{
if (ct.Controls != null && ct.Controls.Count > 0)
{
ReturnControls(ct.Controls, stringControlType);
}
}
}
}
}
#endregion
#region 得到页面中所有指定类型的Control
/// <summary>
/// 使用完方法后可从这个对象得到返回的控件集合
/// </summary>
public ArrayList AlControls;
/// <summary>
/// 得到页面中所有指定类型的Control
/// </summary>
/// <param name="pagecontrols">使用形如this.Controls返回的对象</param>
public void ReturnControls(System.Web.UI.ControlCollection pagecontrols, string stringControlType )
{
if (!string.IsNullOrEmpty(stringControlType)&&pagecontrols.Count>0)
{
foreach(Control ct in pagecontrols)
{
if (ct.GetType().ToString() == stringControlType)
{
this.AlControls.Add(ct);
}
else
{
if (ct.Controls != null && ct.Controls.Count > 0)
{
ReturnControls(ct.Controls, stringControlType);
}
}
}
}
}
#endregion
解决方案 »
- 问个Datatable的一列转换数组的效率问题
- 有人可以帮忙连接一下数据么?
- table控件的使用,问了好几次了,没有满意答案。。。
- 工资表如何处理?
- 请问如何以流的形式将服务器的内的一个对像输出到客户端(如 excel对象) 谢谢
- ASP.NET 代码调试没有错误,但Excel中的数据却插入不了数据库表里面呢?
- 怎样在日历控件中设置上月的背景图?
- 请高手请教一个DataGrid的问题!(高分)
- 查找数据库的某一行是否存在(c#)
- store.exe inetinfo.exe 线程狂吃内存。 为什么???/
- AsyncFileUpload上传失败提示The file attached is invalid是什么原因
- ASP.NET 中有没有只选目录级,不选文件的控件
{
TextBox t = c as TextBox;
if (t != null)
{
string id = t.ID;
}
}
Control[] GetControls(string controlName){}
//想封装到库里的话,自己所这个this.Page对象作参数丢过去,它代表什么我不再多说
foreach (Control con in this.Page.Controls)
{
if (con.GetType() = typeof(LinkButton))
{
//如果控件是LinkButton
}
}
Control[] GetControls(string controlName){}
-------------------------------------------
能说一下你这个怎么具体实现么?过程中一些东西我不是很懂
public static Control[] GetControls(Control c, string controlName)
for(int i=0 ;i<4 i++)
{
this.form1.findcontrol("text"+i.tostring()).enabled=false;
}
List<System.Web.UI.Control> GetControls(System.Web.UI.ControlCollection ctrls, Type t)
{
List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
foreach (System.Web.UI.Control ctrl in ctrls)
{
if (ctrl.GetType() == t)
{
list.Add(ctrl);
}
if (ctrl.Controls.Count > 0)
{
foreach (System.Web.UI.Control c in ctrl.Controls)
{
list.AddRange(GetControls(c.Controls, t));
}
}
}
return list;
}
{
List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
foreach (System.Web.UI.Control ctrl in ctrls)
{
if (ctrl.GetType() == t)
{
list.Add(ctrl);
}
if (ctrl.Controls.Count > 0)
{
foreach (System.Web.UI.Control c in ctrl.Controls)
{
list.AddRange(GetControls(c.Controls, t));
}
}
}
return list;
} protected void Button1_Click(object sender, EventArgs e)
{
List<System.Web.UI.Control> list = GetControls(this.Page.Controls, TextBox1.GetType());
}楼上的,我按上面的调用了,跟踪list,发现始终为0。也就是无效。
List<System.Web.UI.Control> list = GetControls(this.Page.Form.Controls, typeof(TextBox));
不过话说,从发帖到现在已经过了快一个月了,看来你对这个问题也不是很重视嘛
{
List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
foreach (System.Web.UI.Control ctrl in ctrls)
{
if (ctrl.GetType() == t)
list.Add(ctrl);
if (ctrl.Controls.Count > 0)
list.AddRange(GetControls(ctrl.Controls, t));
}
return list;
}
http://www.cnblogs.com/insus/articles/2033414.html
///得到给定的控件下的所有某种类型的子控件。
/// </summary>
/// <param name="c">给定的控件。</param>
/// <param name="controlName">要获取的子控件类型名称。</param>
/// <returns>找到的所有子控件。</returns>
public static Control[] GetControls(Control c, string controlName)
{
List<Control> controls = new List<Control>();
BuildControls(c, controlName, controls);
Control[] controlArray = controls.ToArray();
return controlArray;
} /// <summary>
/// 构建GetControls方法中创建的controls泛型集合,为其添加元素。
/// </summary>
/// <param name="c">给定的控件。</param>
/// <param name="controlName">要获取的子控件类型名称</param>
/// <param name="controls">GetControls方法中创建的controls泛型集合的一个引用。</param>
private static void BuildControls(Control c, string controlName, List<Control> controls)
{
for (int i = 0; i < c.Controls.Count; i++)
{
if (c.Controls[i].GetType().Name == controlName)
controls.Add(c.Controls[i]);
if (c.Controls[i].HasControls())
BuildControls(c.Controls[i], controlName, controls);
}
}
GetControls(this.Page,"TextBox");
获取div1下面的所有Button,返回一个Control类型的数组:
GetControls(div1,"Button");