Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

notify user that changes were made

Status
Not open for further replies.

jimktrains

IS-IT--Management
Jan 19, 2007
50
US
I have a page where a user selects an item and then the page is populated from a database. I set a javascript variable "changed" to false and each field has an onchange event that sets "changed" to true so when the user leaves the page without saving, they will be notified.

But I noticed that after the database populates the fields, the value of "changed" is true, because the values of the field chnaged from blank to a value. I even tried to set "changed" to false using ScriptManager.RegisterClientScriptBlock after all the data loaded but that didn't work.

Using C# in the codebehind, or just using javascript, how can I set changed to false after the data loads?
 
Why dont you change the "changed" control to have a runat="server" attribute.
Then as soon as the controls are loaded, set the "changed" value to true.
To reference the changed control value in javascript, you can do something like when the controls change:
$("#<%=changed.ClientID%>").val("true") with jquery
Or document.getElementById("#<%=changed.ClientID%>").value() = true with pure javascript

Hope this makes some sense

 
I tried having a textbox with an ID of changed and set it to "false" initially. The problem is, when the data is loaded from the database, the onchange event fires and sets it to "true". I've even tried setting the textbox.text to "false" in the codebehind after the data was loaded (by loaded I mean setting each textbox to the values from the database.) But that didn't help. I know how to check the value of the textbox when leaving the page, it's just that it gets set to "true" when the values are loaded into the fields.

Is there some type of event like Page_Unload where I can set the textbox to "false" after all other fields are loaded? I tried Page_Unload to set the textbox but at that point, but it didn't help.
 
I have come up with a solution.

I've added a textBox field called txt_recLoaded that is initially set to "false" with the following line,
if (!IsPostBack) txt_recLoaded.Text = "false";
Notice it is literal "false" and not bool false.
Here's my javascript.

<script type="text/javascript">
var needtoConfirm = true;
var changed = false;
window.onbeforeunload = confirmExit;

function confirmExit() {
if (changed && needtoConfirm) {
changed = false;
return "You have made changes without clicking the Save button, your changes will be lost.";
}
}

function waschanged() {
var rlid = document.getElementById("<%=txt_recLoaded.ClientID %>");
var recLoaded = rlid.value;
if (recLoaded == "false") {
rlid.value = "true";
}
else {
changed = true;
}
}
</script>

On each TextBox, that's all I have for now, I have onchange="waschanged();"
So when the data is loaded into the fields, it initially only sets txt_recLoaded to "true" and not the javascript variable changed. For some reason, waschanged on fires once even though multiple fields are changed when the record loads.

One the initial fields are loaded, and txt_recLoaded is changed to "true", when a field is changed, the javascript changed is set to true, then when leaving the page, they would get a message. But if they save the record, I set changed to false so it doesn't show.

Also, if the user will add a record and not bring up an existing one, I set txt_recLoaded to "true" so any entry would be a change.

Also, needtoConfirm is set to false in situations where the user clicks Cancel and they don't want to save.

And the line "changed = false" in the confirmExit() function was needed because when the user would click a menu button to move to another application, confirmExit fired twice prompting the user both times. This way, it only fires once since the second time, changed=false. Now, this does cause another problem where if the user clicks the Cancel button when prompted with the not saved message, changed is false so if they try to leave right after without making more changes, they get no message at all. I'll try to find a solution for that later, unless someone has a tip on how to process the Cancel event and set change back to true.
 
The problem with your solution is that you are using onbeforeunload which will only work in IE. Other browsers will not recognize that function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top