Hello Programmers,
I'm trying to implement a client side test for a dirty form (i.e form data has changed) . If the data entry person attempts to navigate away from the form without committing the changes, a confirm alert box will pop up to remind the user to save the changes or cancel.
Here is the example client side code without using ASP.NET:
1 <script language="javascript">
2 var isDirty;
3 isDirty = 0;
4 function setDirty() {
5 isDirty = 1;
6 }
7 function checkSave() {
8 var sSave;
9 if (isDirty == 1) {
10 sSave = window.confirm("You have some changes that have not been saved. Click OK to save now or CANCEL to continue without saving.");
11 if (sSave == true) {
12 window.document.frmTest.submit();
13 } else {
14 return true;
15 }
16 }
17 }
18 </script>
19 <body onunload="checkSave()">
20 <h1>Submit Changes Test</h1>
21 <hr noshade size="1">
22 <form name="frmTest" action="" method="post">
23 <font face="Arial">First Name: <input type="text" name="FirstName" value="Robert" size="40" onchange="setDirty()"><br>
24 Second Name: </font> <input type="text" name="SecondName" value="Robbins" size="40" onchange="setDirty()"><br>
25 <input type="submit" name="btnSubmit" value="Submit Form" onclick="isDirty = 0;">
26 </form>
27 <p><font face="Arial"><a href="http://www.yahoo.com">Yahoo</a></font></p>
28
In my attempt to implement this for an ASP.NET page I use this code in the ASPX file:
1 <script language="javascript">
2 var isDirty;
3 isDirty = 0;
4 function setDirty() {
5 isDirty = 1;
6 }
7 function checkSave() {
8 var sSave;
9 if (isDirty == 1) {
10 sSave = window.confirm("You have some changes that have not been saved. Click OK to save now or CANCEL to continue without saving.");
11 if (sSave == true) {
12 window.document.frmEditApp.submit();
13 } else {
14 return true;
15 }
16 }
17 }
18 </script>
19 <body class="StandardBody" onunload="checkSave()">
And this code in the VB code behind file:
1 ' add JavaScript events to web controls
2 btnSubmit.Attributes.Add("onclick", "isDirty = 0;")
3 btnCancel.Attributes.Add("onclick", "isDirty = 0;")
4 ' plaintiff information
5 txtName.Attributes.Add("onchange", "setDirty();")
6 txtAddress.Attributes.Add("onchange", "setDirty();")
It works except the JavaScript to submit the form does not appear to trigger the server side button's click event so the data does not get saved. I cannot figure out how to get the client-side JavaScript to fire the server side event btnSubmit_Click. Do I need to use a client callback? I have been unable to find any sample code to accomplish this requirement.
Robert S. Robbins
Web Developer
http://www.williamsportwebdeveloper.com/