对 ASP.net 服务器控件进行分组以修改属性,而无需按名称引用它们

发布于 2024-12-10 17:57:35 字数 424 浏览 0 评论 0原文

我正在尝试从我的 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 技术交流群。

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

发布评论

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

评论(3

别闹i 2024-12-17 17:57:35

当然。您可以通过构建 List 对它们进行分组,然后对其进行循环。

var controlList = new List<Control>();
controls.Add(someTextBox);
controls.Add(someOtherTextBox);
// etc.

// elsewhere
foreach (var control in controlList)
    control.Visible = false;

当然,如果您正在处理全部保存在公共容器(表单、面板等)中的控件,则可以简单地循环遍历其控件集合。

foreach (var control in theContainer.Controls)
    control.Visible = false;

在这个简单的例子中,使容器本身不可见就可以完成这项工作。除了这些简单的方法之外,据我所知,没有一种方法可以对控件进行分组,但我并不经常处理 UI。

Sure. You can group them by building a List<Control>, and then loop over it.

var controlList = new List<Control>();
controls.Add(someTextBox);
controls.Add(someOtherTextBox);
// etc.

// elsewhere
foreach (var control in controlList)
    control.Visible = false;

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.

foreach (var control in theContainer.Controls)
    control.Visible = false;

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.

陌生 2024-12-17 17:57:35

如果将它们放入某种容器中,例如面板或占位符,则可以执行以下操作:

List<WebControl> ctrlList = PlaceHolder1.Controls.Cast<WebControl>().ToList();
foreach (WebControl ctrl in ctrlList)
{
    ctrl.Visible = false;
}

您可以通过执行以下操作来缩短上面的示例:

PlaceHolder1.Controls.Cast<WebControl>().ToList().ForEach(x => x.Visible = false);

如果您愿意,您可以创建返回不同类型的列表的属性控制:

public List<TextBox> InputList
{
    get
    {
        return PlaceHolder1.Controls.OfType<TextBox>().ToList();
    }
}

If you put them in a container of some sort, like a Panel or PlaceHolder, you can do something like this:

List<WebControl> ctrlList = PlaceHolder1.Controls.Cast<WebControl>().ToList();
foreach (WebControl ctrl in ctrlList)
{
    ctrl.Visible = false;
}

You can shorten the above example by doing this:

PlaceHolder1.Controls.Cast<WebControl>().ToList().ForEach(x => x.Visible = false);

If you wanted to, you could create properties that return lists of different types of controls:

public List<TextBox> InputList
{
    get
    {
        return PlaceHolder1.Controls.OfType<TextBox>().ToList();
    }
}
关于从前 2024-12-17 17:57:35

有多种方法可以做到这一点,具体取决于您指定的项目/需求。

您可以将一组项目包装在 中,然后使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文