简体   繁体   English

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

[英]ASP.NET find all Controls on a page and hide them

I'm trying to on Page_Load hide all my RadioButtonLists but I can't seem to get the syntax quite right 我正试图在Page_Load上隐藏我所有的RadioButtonLists,但我似乎无法使语法完全正确

I'm guessing I've got to use the FindControl syntax something like this 我猜我必须使用像这样的FindControl语法

CType(FindControl, RadioButtonList)

And then I'm guessing I will have to loop through each RadioButtonList and set the Visible = False attribute on it. 然后我猜我将不得不遍历每个RadioButtonList并在其上设置Visible = False属性。

I seem to be getting an error with the code above. 我似乎在上面的代码中出错了。

Any ideas what I can try? 我有什么想法可以尝试吗?

Thanks 谢谢

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);
    }
}

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. FindControl仅在您知道要查找的控件的名称时才有效,而且不仅仅是递归调用。 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. 如果要查找所有radiobutton列表,则需要编写一个循环遍历父/子关系中所有控件集的方法,并将radiobuttonlist设置为false。

Just pass Page.Controls to this function (untested, may need tweaking): 只需将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;
    }
}

Why not use a ASP.Net skin page to set the default values for all RadioButtonLists to visible = false. 为什么不使用ASP.Net皮肤页面将所有RadioButtonLists的默认值设置为visible = false。

I would def look into using a skin page here. 我会在这里使用皮肤页面。

Doing a foreach on the Controls property and checking the type is going to be slow. 在Controls属性上执行foreach并检查类型将变慢。 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. 在我看来,根据您的要求,您应该做的是使用CSS /皮肤隐藏不需要的按钮,或者只是将它们添加到List<T>这样您就可以只遍历需要修改的按钮。

Worst case scenario the foreach will work but it's a bit slow and undesirable. 最糟糕的情况是foreach会起作用,但它有点慢而且不受欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM