访问 aspx 页面中的控件
我们有一些代码旨在在 ASP.NET Web 表单页面上构建键/值对控件及其值。我们使用“Controls”集合和递归来构建这个键/值对。
唯一的问题是 ascx 控件似乎没有返回“控件”集合中。相反,我们获得了 ascx 上的控件。
类似于以下代码:
protected override void OnLoad(EventArgs e)
{
GetControls(this);
}
protected void GetControls(System.Web.UI.Control ctl)
{
foreach (Control subCtl in ctl.Controls)
{
GetControls(subCtl);
string value;
if (GetValueFromControl(subCtl, out value))
{
_ControlValues.Add(subCtl.ClientID, value);
}
}
}
subCtl 绝不是 ascx 控件。有没有办法获取页面上的 ascx 控件列表,或者更好的是获取包含这两种类型的不同集合?
谢谢
汉斯
We have some code which is designed to build up a key/value pair of controls on a ASP.NET webforms page and their values. We use the "Controls" collection and recursion to build up this key/value pair.
The only issue is that ascx controls don't appear to come back in the "Controls" collection. Instead we get the controls on the ascx.
Something similar to the following code:
protected override void OnLoad(EventArgs e)
{
GetControls(this);
}
protected void GetControls(System.Web.UI.Control ctl)
{
foreach (Control subCtl in ctl.Controls)
{
GetControls(subCtl);
string value;
if (GetValueFromControl(subCtl, out value))
{
_ControlValues.Add(subCtl.ClientID, value);
}
}
}
The subCtl is never a ascx control. Is there a way to get a list of the ascx controls on page or better still a different collection that contains both types?
Thanks
Hans
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.ascx 控件确实出现在控件集合中。
查看您的代码,我想知道这是否可能是 GetValueFromControl 函数的问题 - 也许它对于 .ascx 控件无法正常工作,或者您正在寻找的任何值未设置?或者,也许实际代码中的等效递归函数只是将叶节点添加到 _ControlValues,并跳过具有子节点的节点?
.ascx controls do appear in the controls collection.
Looking at your code, I wonder if this is perhaps an issue with the GetValueFromControl function -- perhaps it's not functioning correctly for .ascx controls, or whatever value you're looking for is not set? Or perhaps the equivalent recursion function in your real code is only adding leaf nodes to _ControlValues, and skipping over nodes with children?
据我所知,ASCX 控件包含在
Controls
集合中。我创建了一个继承
System.Web.UI.WebPage
的类,它将迭代this.Controls
中的所有控件,并且它包含所有 ASCX 类。As far as I know, ASCX controls are included in the
Controls
collection.I've made a class inheriting off of
System.Web.UI.WebPage
that would iterate through all of the controls inthis.Controls
and it included all of the ASCX classes.