ASP.NET 查找页面上的所有控件并隐藏它们

发布于 2024-10-16 06:39:52 字数 308 浏览 6 评论 0原文

我试图在 Page_Load 上隐藏我所有的 RadioButtonLists 但我似乎无法完全正确地使用语法

我猜我必须使用像这样的 FindControl 语法

CType(FindControl, RadioButtonList)

然后我'我猜我必须循环遍历每个 RadioButtonList 并在其上设置 Visible = False 属性。

我似乎在上面的代码中遇到了错误。

我可以尝试什么想法吗?

谢谢

I'm trying to on Page_Load hide all my RadioButtonLists but I can't seem to get the syntax quite right

I'm guessing I've got to use the FindControl syntax something like this

CType(FindControl, RadioButtonList)

And then I'm guessing I will have to loop through each RadioButtonList and set the Visible = False attribute on it.

I seem to be getting an error with the code above.

Any ideas what I can try?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

栖迟 2024-10-23 06:39:53

试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    HideRadioButtonLists(Page.Controls);
}

private void HideRadioButtonLists(ControlCollection controls)
{
    foreach (WebControl control in controls.OfType<WebControl>())
    {
        if (control is RadioButtonList)
            control.Visible = false;
        else if (control.HasControls())
            HideRadioButtonLists(control.Controls);
    }
}

Try this:

protected void Page_Load(object sender, EventArgs e)
{
    HideRadioButtonLists(Page.Controls);
}

private void HideRadioButtonLists(ControlCollection controls)
{
    foreach (WebControl control in controls.OfType<WebControl>())
    {
        if (control is RadioButtonList)
            control.Visible = false;
        else if (control.HasControls())
            HideRadioButtonLists(control.Controls);
    }
}
挽心 2024-10-23 06:39:53

FindControl 仅在您知道要查找的控件的名称时才有效,而且它不是递归调用。除非您能保证您的控件位于您正在搜索的特定容器中,否则您将找不到它。如果要查找所有单选按钮列表,则需要编写一个方法,循环遍历父/子关系中的所有控件集,并将单选按钮列表可见设置为 false。

只需将 Page.Controls 传递给此函数(未经测试,可能需要调整):

public void HideRadioButtonLists(System.Web.UI.ControlCollection controls)
{
    foreach(Control ctrl in controls)
    {
        if(ctrl.Controls.Count > 0) HideRadioButtonLists(ctrl.Controls);
        if("RadioButtonList".Equals(ctrl.GetType().Name, StringComparison.OrdinalIgnoreCase))
            ((RadioButtonList)ctrl).Visible = false;
    }
}

FindControl only works if you know the name of the control you're looking for, and more than that it is not a recursive call. Unless you can guarantee that your control will be in the specific container you're searching in, you won't find it. If you want to find all of the radiobutton lists, you'll need to write a method that cycles through all control sets in the parent/child relationship and sets the radiobuttonlist visible to false.

Just pass Page.Controls to this function (untested, may need tweaking):

public void HideRadioButtonLists(System.Web.UI.ControlCollection controls)
{
    foreach(Control ctrl in controls)
    {
        if(ctrl.Controls.Count > 0) HideRadioButtonLists(ctrl.Controls);
        if("RadioButtonList".Equals(ctrl.GetType().Name, StringComparison.OrdinalIgnoreCase))
            ((RadioButtonList)ctrl).Visible = false;
    }
}
帅气尐潴 2024-10-23 06:39:53

为什么不使用 ASP.Net 皮肤页面将所有 RadioButtonList 的默认值设置为visible = false。

我肯定会考虑在这里使用皮肤页面。

Why not use a ASP.Net skin page to set the default values for all RadioButtonLists to visible = false.

I would def look into using a skin page here.

执笏见 2024-10-23 06:39:53

对 Controls 属性执行 foreach 并检查类型会很慢。在我看来,根据您的要求,您应该做的是使用 CSS / 皮肤来隐藏不需要的按钮,或者简单地将它们添加到 List 中,这样您就可以只循环遍历那些你需要修改。

最坏的情况是 foreach 可以工作,但有点慢而且不受欢迎。

Doing a foreach on the Controls property and checking the type is going to be slow. What you should be doing, in my opinion and depending on your requirements, is using CSS / skins to hide the unwanted buttons or simply adding them to a List<T> so you can loop through only those that you need to modify.

Worst case scenario the foreach will work but it’s a bit slow and undesirable.

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