BreakHead BreakHead - 11 months ago 43
ASP.NET (C#) Question

Enable/Disable <asp:Panel> and all its control using Javascript

I have

on my page. At run time, I add controls to this panel, and I want to disable/enable all its controls as per business logic.

I tried with this:

document.getElementById('mypanel').disabled = true;

But it is not working.

Does anyone has any idea to make this work?

Answer Source

An asp:Panel just produces a div element. This isn't a form control, it's just there for structure.

To disable every input control inside of it, if you are using jQuery, try:

$("#<%=mypanel.ClientID%> input").attr("disabled", true);

Or plain ol' JavaScript:

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

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