通过ASP循环:CheckBox控件 [英] Looping through asp:CheckBox controls

查看:119
本文介绍了通过ASP循环:CheckBox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些控件11我的网页上,都是复选框。它包含一个母版页中。

I have 11 of these controls on my page, all are checkboxes. It is contained within a master page.

我可以完成我想,像这样:

I can accomplish what I want like so:

generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation2.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation3.InputAttributes.Add( "class", "SetupChecklist" );

我现在通过这些努力环和做同样的事情对自己节省一些code,但我有很多的麻烦为此才能正常工作,以及我不能让它在所有的工作

I am now trying to loop through these and do the same thing to save myself some code, but I am having a lot of trouble getting this to work properly, well I can't get it to work at all.

谁能给我一个很好的方式依次通过这11 CheckBox控件,并添加CSS类SetupChecklist?

Can anyone give me a good way to loop through these 11 checkbox controls and add the css class SetupChecklist?

我尝试这样做,它不添加类的某些原因。

I tried this and it isn't adding the class for some reason.

protected void InitializeCheckboxes ()
    {
        //generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
        var allCheckBoxes = Page.Controls.OfType<CheckBox>();
        foreach ( var c in allCheckBoxes )
        {
            c.InputAttributes.Add( "class", "SetupChecklist" );
        } 
    }

我去叫 InitializeCheckboxes(); 在Page_Load方法。它不工作的时候我只是用generalInformation.InputAttribues.Add等。但不是当我遍历他们。有什么建议?

I go call InitializeCheckboxes(); in the Page_Load method. It does work when I just use generalInformation.InputAttribues.Add etc.. But not when I loop through them. Any suggestions?

推荐答案

最好将它们放置在一个面板(呈现为一个div)或其他容器控件。然后你可以用LINQ的 OfType

The best would be to place them in a Panel(rendered as a div) or other container control. Then you can get the references with LINQ's OfType:

// assuming all checkboxes are in a panel named "SetupContainer"
var allCheckBoxes = SetupContainer.Controls.OfType<CheckBox>();
foreach(var chb in allCheckBoxes)
    chb.InputAttributes.Add( "class", "SetupChecklist" );

当然,你也可以用它来发现整个页面上的所有复选框但可能是容易出错。

Of course you can also use it to find all CheckBoxes on the whole Page, but that might be prone to errors.

这篇关于通过ASP循环:CheckBox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆