递归查找页面控件并添加属性?

发布于 2024-10-31 08:23:48 字数 1383 浏览 0 评论 0原文

我需要一个递归函数来查找页面上的所有控件,并允许我根据控件类型添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-11-07 08:23:48

这应该有效:

public void traverse(Control ctl)
{
    foreach (Control c in ctl.Controls) 
    {
        System.Diagnostics.Debug.WriteLine(c.GetType().ToString());
        //Response.Write(c.GetType().ToString() + " : " + c.ID.ToString() + "<br />"); 
        if (c.GetType() == typeof(TextBox)) 
        { ((TextBox)(c)).Attributes["onKeypress"] = "javascript:return FormEdited();"; 
        } 
        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(c);
    }
}

然后用以下方式调用它:

traverse(this.Page);

ie

protected void Page_Load(object sender, EventArgs e)
{
   traverse(this.Page);
}

This should work:

public void traverse(Control ctl)
{
    foreach (Control c in ctl.Controls) 
    {
        System.Diagnostics.Debug.WriteLine(c.GetType().ToString());
        //Response.Write(c.GetType().ToString() + " : " + c.ID.ToString() + "<br />"); 
        if (c.GetType() == typeof(TextBox)) 
        { ((TextBox)(c)).Attributes["onKeypress"] = "javascript:return FormEdited();"; 
        } 
        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(c);
    }
}

and then call it with:

traverse(this.Page);

i.e.

protected void Page_Load(object sender, EventArgs e)
{
   traverse(this.Page);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文