递归查找页面控件并添加属性?
我需要一个递归函数来查找页面上的所有控件,并允许我根据控件类型添加 javascript 控件属性。
问题是我有一个页面,其中有几个带有控件的面板。这些面板甚至可以有嵌套的面板/控件。
不幸的是,以下内容没有达到我想要的效果,但我正在寻找类似的东西......
Action<Control> traverse = null;
//in a function:
traverse = (ctrl) =>
{
//ctrl.Enabled = false; //or whatever action you're performing
foreach (Control c in ctrl.Controls)
{
Response.Write(c.GetType().ToString() + " : " + c.ID.ToString() + "<br />");
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Attributes["onKeypress"] = "javascript:return FormEdited();";
}
else if (c.GetType() == typeof(DropDownList))
{
((DropDownList)(c)).Attributes["onchange"] = "javascript:return FormEdited();";
}
else if (c.GetType() == typeof(CheckBox))
{
((CheckBox)(c)).Attributes["onClick"] = "javascript:return FormEdited();";
}
}
traverse = (ctrl2) => ctrl.Controls.GetEnumerator();
};
I need a recursive function that will find all controls on a page and allow me to add javascript control attributes based on the control type.
The issue is that I have a page with several panels which have controls. The panels could even have nested panels/controls.
Unfortunately the following doesn't do what I want, but I'm looking for something similar....
Action<Control> traverse = null;
//in a function:
traverse = (ctrl) =>
{
//ctrl.Enabled = false; //or whatever action you're performing
foreach (Control c in ctrl.Controls)
{
Response.Write(c.GetType().ToString() + " : " + c.ID.ToString() + "<br />");
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Attributes["onKeypress"] = "javascript:return FormEdited();";
}
else if (c.GetType() == typeof(DropDownList))
{
((DropDownList)(c)).Attributes["onchange"] = "javascript:return FormEdited();";
}
else if (c.GetType() == typeof(CheckBox))
{
((CheckBox)(c)).Attributes["onClick"] = "javascript:return FormEdited();";
}
}
traverse = (ctrl2) => ctrl.Controls.GetEnumerator();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
然后用以下方式调用它:
ie
This should work:
and then call it with:
i.e.