View Full Version : Disable invisible controls

11-03-2004, 06:14 PM
I'm building an application in asp.net where only a portion of the controls on a page is visible, depending of the access of the user logged in at the moment.
On some pages I have radiobuttons to enable a part of a page, and disable another. The enabling/disabling is done with javascript. The problem is that the javascript is not able to disable/enable controls that are invisible at the moment. Why is that so?

I guess I could make several disable/enable scripts depending on the access of the user, but I would rather not.

Thankful for any tip I can get.

11-03-2004, 07:23 PM
Can you give us something to work with? A link to the scripts in action, or at least a codee sample?

11-04-2004, 08:49 AM
Of course.

My .aspx-file contains among other things two radiobuttons, and they call a javascript function enableChangeInfo(param) as their onclick-method. One of them send true as param and the other one false.

My script looks as follows:

function enableChangeInfo(param)
document.getElementById('lblIntervalStart').disabled = param;
document.getElementById('txtIntervalStart').disabled = param;
document.getElementById('imgbtnIntervalStart').disabled = param;
document.getElementById('radioIntervalStart').disabled = param;
document.getElementById('lblIntervalEnd').disabled = param;
document.getElementById('txtIntervalEnd').disabled = param;
document.getElementById('imgbtnIntervalEnd').disabled = param;
document.getElementById('radioIntervalEnd').disabled = param;
document.getElementById('chkOvertime').disabled = param;
document.getElementById('cboOvertime').disabled = param;
document.getElementById('lblOvertime').disabled = param;
document.getElementById('chkWeekend').disabled = param;
document.getElementById('chkFix').disabled = param;
document.getElementById('lblComment').disabled = param;
document.getElementById('txtComment').disabled = param;

The five controls chkOvertime, cboOvertime, lblOvertime, chkWeekend and chkFix are either visible or invisible depending on the user who is watching the page. Their visibility is set in the page_load method in the codebehind file.

When I run the application with all the controls visible (administrator mode), the script works fine, but when one or more of the controls mentioned above is made invisible, the script debugger says that:
"document.getElementById()' is null or not an object".

Can I work around this in some way?

Thanks in advance

11-08-2004, 08:27 PM
I don't know if you've spotted this, but if a user (who is not an administrator) was to simply copy your source code and make the controls visible, they could make the changes they want and submit the form to your server. Therefore, even though they are not an Admin, they could still make use of all of the controls.
In fact, to users without JavaScript enabled, they would see all of the admin controls anyway.

It would be much safer if you were to "hide" the controls using ASP.net by either writing or not writing those input fields to the page. If you were to do that then your current problem should just disappear.