对 ASP.net 服务器控件进行分组以修改属性,而无需按名称引用它们
我正在尝试从我的 aspx 页面的代码隐藏中动态设置多个框、面板、按钮等的 Visible
属性。我想知道是否有一种方法可以将服务器控件分组到某个逻辑组中,以便您可以执行类似伪代码的操作:
foreach(c in controlgroup) {
c.Visible = value
}
原因是页面根据 URL 中的 ID 参数显示不同类型的选项。我已经看到这个问题,但我想知道除了使用之外是否还有其他方法可以做到这一点用户控制?
编辑: 如果我可以在 aspx 页面上设置组而不必手动添加每个控件,那就太好了。
I'm trying to dynamically set the Visible
property of multiple boxes, panels, buttons etc. from my codebehind for an aspx page. I was wondering if there is a way to group server controls into some logical group so that you could do something like this pseudo-code:
foreach(c in controlgroup) {
c.Visible = value
}
The reason being is that the page displays different kinds of options depending on an ID parameter in the URL. I have seen this question but I'm wondering if there's another way to do it than by using user controls?
Edit:
It would be nice if I could set the group on the aspx page rather than having to still add each control manually..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。您可以通过构建
List
对它们进行分组,然后对其进行循环。当然,如果您正在处理全部保存在公共容器(表单、面板等)中的控件,则可以简单地循环遍历其控件集合。
在这个简单的例子中,使容器本身不可见就可以完成这项工作。除了这些简单的方法之外,据我所知,没有一种方法可以对控件进行分组,但我并不经常处理 UI。
Sure. You can group them by building a
List<Control>
, and then loop over it.Of course, if you were working on controls all held in a common container (form, panel, etc.), you could simply loop over its control collection.
And in this trivial example, making the container itself invisible would do the job. Beyond these simple methods, there is not a way to group controls that I know of, but I'm not often dealing with the UI.
如果将它们放入某种容器中,例如面板或占位符,则可以执行以下操作:
您可以通过执行以下操作来缩短上面的示例:
如果您愿意,您可以创建返回不同类型的列表的属性控制:
If you put them in a container of some sort, like a Panel or PlaceHolder, you can do something like this:
You can shorten the above example by doing this:
If you wanted to, you could create properties that return lists of different types of controls:
有多种方法可以做到这一点,具体取决于您指定的项目/需求。
您可以将一组项目包装在
中,然后使用 Children 集合。您可以为控件提供特殊的 ID,如 group1_name、group2_name 等,并循环页面的子控件集合
您可以向控件添加属性,如
并循环页面的子控件,然后检查属性。您可以添加基类并监视子集合并使用 .Tag 属性来分配组
等等...
There are various ways to do this and depends on your specified project/needs.
You could wrap a group of items in a
<asp:Panel>
and then use the Children collection.You could give the controls special ids like, group1_name, group2_name etc and loop the page's child control collection
You could add attributes to your controls like
<asp:Button ... myGroup="1" />
and loop the Page's child controls then check the attributes.You could add a base class and monitor the children collection and use the .Tag property to assign groups
and on and on...