[RESOLVED]How to Make a Javascript Function Run ONLY if a Control is Enabled

ASP.Net 4.5.  IIS 7.  Visual Studio 2012.  JavaScript.

I have placed a JavaScript function at the top of my asp.net (aspx) page.  It runs when the page is opened (and produces an error because the EnterForm (formview) control it references is not enabled).  How can I make this function run its code ONLY if the
control is enabled?  Here is the code:

        function calcTotalHours() {
                var TotalHours = document.getElementById('<%= EnterForm.Findcontrol("TotalFacHoursTextBox").ClientId %>');
                var Hours = ['<%= EnterForm.Findcontrol("SundayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("MondayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("TuesdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("WednesdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("ThursdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("FridayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("SaturdayHoursTextBox").ClientId %>'];
                var total = 0;
                for (var i = 0; i <= 6; i++) {
                    if (!isNaN(document.getElementById(Hours[i]).value) && document.getElementById(Hours[i]).value != "") {
                        total += eval(document.getElementById(Hours[i]).value);
                    }
                }
                TotalHours.value = total;
        }

Hi Chaplain,

Thanks for your post.

Firstly, I’d like to know when do you call calcTotalHours() function?

Chaplain Doug

It runs when the page is opened (and produces an error because the EnterForm (formview) control it references is not enabled). 

Secondly, what is the error you got? It will help us understand your problem.

Thirdly, we could check the value of “TotalHours” and “Hours” when the EnterForm (formview) control is not enabled. May be the value is null (or undefined), then we could modify the code to determine whether the EnterForm is enabled or not, and make javascript
function run only if the EnterForm is enabled.

The code may look like as below.

function calcTotalHours() {
    var TotalHours = document.getElementById('<%= EnterForm.Findcontrol("TotalFacHoursTextBox").ClientId %>');
    var Hours = ['<%= EnterForm.Findcontrol("SundayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("MondayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("TuesdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("WednesdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("ThursdayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("FridayHoursTextBox").ClientId %>', '<%= EnterForm.Findcontrol("SaturdayHoursTextBox").ClientId %>'];

    if (TotalHours == "null" || Hours == "null") {
        return false;
    }

    var total = 0;
    for (var i = 0; i <= 6; i++) {
        if (!isNaN(document.getElementById(Hours[i]).value) && document.getElementById(Hours[i]).value != "") {
            total += eval(document.getElementById(Hours[i]).value);
        }
    }
    TotalHours.value = total;
} 

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply