Question : How to prevent a JavaScript function from been executed except in a specific cases?

Dear Experts,

First of all I'm a beginner  in java script and I have a problem which I understand but I don't know how fix it.

I have a web form (ASPX page) which contain four buttons (New, Save, Delete, Undo), some text boxes and radio buttons.  I want to enable/disable the four buttons depending on the situations of the user action. When the page requested for the first time and there is no value passed to the page via Request.QueryString that mean it's a new record state so I enable (NEW, Undo) buttons and disable (Save, Delete) buttons. When the user types a something in any Textbox or clicked on a radio button I have a JavaScript function which will be called to reverse the buttons enable/disable state.

My problem is the java function keep executing in case the user types anything or when I move string. Empty to the text boxes to make them ready for the user to enter the required data. Text change event keep firing in both ways which cause the java function to be executed.

How can I control this behavior so the java function will be executed only when the user types something or clicked on one of the radio buttons?

Thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
JavaScript Function:
function ControlChanges() {
 var BtnNew = document.getElementById('<%=Me.BtnNew.ClientID%>');
 var BtnSave = document.getElementById('<%=Me.BtnSave.ClientID%>');
 var BtnDelete = document.getElementById('<%=Me.BtnDelete.ClientID%>');
 var BtnUndo = document.getElementById('<%=Me.BtnUndo.ClientID%>');
    BtnNew.disabled = true;
    BtnDelete.disabled = true;
    BtnSave.disabled = false;
    BtnUndo.disabled = false;
}
 
Page_load Event:
 Me.txtLoginId.Attributes.Add("onpropertychange", "ControlChanges()")
 Me.txtUserName.Attributes.Add("onpropertychange", "ControlChanges()")
 Me.txtEmail.Attributes.Add("onclick", "ControlChanges()")
 Me.RadLanguageArabic.Attributes.Add("onclick", "ControlChanges()")
 Me.RadLanguageEnglish.Attributes.Add("onclick", "ControlChanges()")
 Me.chkIsApplicationsAdmin.Attributes.Add("onclick", "ControlChanges()")
Open in New Window Select All

Answer : How to prevent a JavaScript function from been executed except in a specific cases?

"onpropertychange" event fires when the properties of an object change, such as value, style etc, no matter if it is changed through key strokes or code. It explains why your ClearField will fire this event. But when you click a Radio button, there is no property change, so "event.propertyName" is empty, it explains why it does not enable your buttons when you have "event.propertyName=='value'" in your JavaScript code.

"onkeyup" fires when a key is pressed and then released by the user. So even your ClearField clears the text boxes, but it is not caused by user's key strokes, the event will not get fired.

Hope it helps you understand the problem.

Random Solutions  
 
programming4us programming4us