开发者

How to enable/disable many controls together?

开发者 https://www.devze.com 2023-03-25 18:20 出处:网络
I have many checkBoxes HTML controls runat server, I want to disable and enable them together. I\'ve tried to set them in an ASP.Net Panel and set the panel disabled, but they stayed enabled.

I have many checkBoxes HTML controls runat server, I want to disable and enable them together. I've tried to set them in an ASP.Net Panel and set the panel disabled, but they stayed enabled.

Any idea ?

The code

<asp:Panel runat="server" ID="PrivilegesCheckList" >
    <input id="adminPrivilegeCheckBox" type="开发者_如何学JAVAcheckbox" runat="server" />
    <asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:Resource, itemAdminPrivilege%>" />
    <br />
    <input id="accountPrivilegeCheckBox" type="checkbox" runat="server" clientidmode="Static" />
    <asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:Resource, itemAccountManagerPrivilege%>" />
    <br />
    <input id="employeePrivilegeCheckBox" type="checkbox" runat="server" clientidmode="Static" />
    <asp:Literal ID="Literal3" runat="server" Text="<%$ Resources:Resource, itemEmployeeManagerPrivilege%>" />
    <br />
    <input id="orgChartPrivilegeCheckBox" type="checkbox" runat="server" clientidmode="Static" />
    <asp:Literal ID="Literal4" runat="server" Text="<%$ Resources:Resource, itemOrgChartPrivilege%>" />
</asp:Panel>


Using JavaScript you may do something like this :

var controls = document.getElementById("<%=panel1.ClientID%>").getElementsByTagName("input");

for (var i = 0; i < controls.length; i++)
     controls[i].disabled = true;

Note that ASP.Net produces dynamic Id's for server side controls (panel here ) , that tends to use document.getElementById("<%=panel1.ClientID%>") above.


Setting the Panel to disabled would work for asp Controls like CheckBox but not for an input with runat=server. This checkbox is disabled because the panel has Enabled=false:

<asp:Panel ID="Panel1" runat="server" Enabled="false" >
    <asp:CheckBox ID="CheckBox1" runat="server" /> 
</asp:Panel>

You could also easily fix this with jquery (client side):

$('#mypanelClientID input[type=checkbox]').attr('disabled', true);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号