jcwalker
asked on
Method to autosave a form at regular intervals
Hi,
I have the following code, which is a simple web front end to a series of txt files in a directory. I have improving on this code and want to make it automatically submit the form at regular intervals. Found a javascript function to do this, but not sure if im on the right track and cant seem to get any javascript function to work. Maybe there is a much easier way to do this using something built into Dot Net.
I have the following code, which is a simple web front end to a series of txt files in a directory. I have improving on this code and want to make it automatically submit the form at regular intervals. Found a javascript function to do this, but not sure if im on the right track and cant seem to get any javascript function to work. Maybe there is a much easier way to do this using something built into Dot Net.
<%@ Page Language="VB" Strict="true" validateRequest="false" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' When the page first loads, fill the DropDownList
' with the list of existing notes.
myForm.Attributes.Add("Onload", "test()")
If Not Page.IsPostBack Then
LoadNoteList("")
'Load Default Note
txtNoteText.InnerText= ReadNoteFromFile("TODO")
txtNoteName.text="TODO"
End If
End Sub
Protected Sub ddlNotes_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
' Set the value of the note name textbox.
txtNoteName.Text = ddlNotes.SelectedValue
' If no note is selected clear the textarea, otherwise
' load it with the contents of the selected note.
If ddlNotes.SelectedValue = "" Then
txtNoteText.InnerText = ""
Else
txtNoteText.InnerText = ReadNoteFromFile(ddlNotes.SelectedValue)
End If
End Sub
Protected Sub btnDeleteNote_Click(ByVal sender As Object, ByVal e As System.EventArgs)
DeleteNoteFile(ddlNotes.SelectedValue)
' Reload the note list and set to the default selection
' since the current note no longer exists.
LoadNoteList("")
End Sub
Protected Sub btnSaveNote_Click(ByVal sender As Object, ByVal e As System.EventArgs)
WriteNoteToFile(txtNoteName.Text, txtNoteText.InnerText)
' Reload the note list to include the new note we may have
' just created. The parameter specifies that the note just
' saved should be made the selected note in the DropDownList.
LoadNoteList(txtNoteName.Text)
End Sub
Sub LoadNoteList(ByVal strSelectedNote As String)
Dim I As Integer
Dim arrFiles() As FileInfo
Dim myDirInfo As New DirectoryInfo(Server.MapPath("notes/"))
Dim liSelected As ListItem
' Get a list of all .txt files in the notes folder.
arrFiles = myDirInfo.GetFiles("*.txt")
' Clear the list and repopulate. The first entry is blank.
' The others each correspond to a .txt file in the notes folder.
ddlNotes.Items.Clear()
ddlNotes.Items.Add("")
For I = LBound(arrFiles) To UBound(arrFiles)
ddlNotes.Items.Add(Replace(arrFiles(I).Name.ToString, ".txt", ""))
Next I
' If a note/file is loaded make that one the selected entry.
liSelected = ddlNotes.Items.FindByText(strSelectedNote)
If Not liSelected Is Nothing Then liSelected.Selected = True
End Sub
Sub WriteNoteToFile(ByVal strNoteName As String, ByVal strNoteText As String)
Dim objStreamWriter As StreamWriter
' Get a handle on the file to write to
' and connect it to the StreamWriter object
objStreamWriter = File.CreateText(Server.MapPath("notes/" & strNoteName &
".txt"))
' Write text
objStreamWriter.Write(strNoteText)
' Flush to disk and close file
objStreamWriter.Flush()
objStreamWriter.Close()
End Sub
Function ReadNoteFromFile(ByVal strNoteName As String) As String
Dim objStreamReader As StreamReader
Dim strFileContents As String
' Get a handle on the file to read from
' and connect it to the StreamReader object
objStreamReader = File.OpenText(Server.MapPath("notes/" & strNoteName & ".txt"))
' Read the whole file and close the StreamReader
strFileContents = objStreamReader.ReadToEnd()
objStreamReader.Close()
' Set the return value of our function
ReadNoteFromFile = strFileContents
End Function
Sub DeleteNoteFile(ByVal strNoteName As String)
' Delete the appropriate note file
File.Delete(Server.MapPath("notes/" & strNoteName & ".txt"))
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Notepad</title>
<script language=JavaScript src=autosave.js></script>
<script language=JavaScript>
function test()
{
window.popup("hello");
}
</script>
</head>
<body>
<form id="myForm" runat="server">
<asp:DropDownList ID="ddlNotes" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlNotes_SelectedIndexChanged"
/>
<asp:Button ID="btnDeleteNote" Text="Delete Note" runat="server"
OnClick="btnDeleteNote_Click"
/>
<br /><br />
Note Name: <asp:TextBox ID="txtNoteName" width="255" runat="server" />
<asp:Button ID="btnSaveNote" Text="Save Note" runat="server"
OnClick="btnSaveNote_Click"
/><br />
<TextArea id="txtNoteText" cols="80" rows="30" runat="server" />
</form>
</body>
</html>
Here is the javascript function I found:
var mnAutoSaveMilliSeconds=0;
var mnAutoSaveMilliSecondsExp=0;
var mnAutoSaveInterval=30000;
function AutoSaveInit(nMilliSeconds)
{
try
{
var nMinutes=0;
AutoSaveClearTimeOuts();
nMinutes = ((nMilliSeconds / 1000) / 60);
mnAutoSaveMilliSeconds = nMilliSeconds;
mnAutoSaveMilliSecondsExp=0;
oTimeOut = window.setTimeout("AutoSaveSubmit()",nMilliSeconds);
oInterval = window.setInterval("AutoSaveCountDown()",mnAutoSaveInterval);
document.getElementById("divAutoSave").innerHTML = "<b>Auto Save In " + nMinutes + " Minutes</b>";
}
catch (exception)
{
if (exception.description == null) { alert("AutoSaveInit Error: " + exception.message); }
else { alert("AutoSaveInit Error: " + exception.description); }
}
}
function AutoSaveCountDown()
{
var nMinutesLeft=0;
var nMilliSecondsLeft=0;
mnAutoSaveMilliSecondsExp = mnAutoSaveMilliSecondsExp + mnAutoSaveInterval;
if ( mnAutoSaveMilliSeconds > mnAutoSaveMilliSecondsExp)
{
nMilliSecondsLeft = mnAutoSaveMilliSeconds - mnAutoSaveMilliSecondsExp;
nMinutes= AutoSaveRoundNumber(((nMilliSecondsLeft / 1000) / 60),2);
document.getElementById("divAutoSave").innerHTML = "<b>Auto Save In " + nMinutes + " Minutes</b>";
}
}
function AutoSaveBeforeSubmit()
{
document.getElementById("divAutoSave").innerHTML = '<b>Saving data...Please wait.</b>';
return true;
}
function AutoSaveClearTimeOuts()
{
try
{
window.clearInterval(oInterval);
window.clearTimeout(oTimeOut);
}
catch (exception) { }
}
function AutoSaveSubmit()
{
try
{
AutoSaveClearTimeOuts();
AutoSaveBeforeSubmit();
/*
Call the form submittal code in your main page.
*/
SubmitFormToBeSaved();
}
catch (exception) {}
}
function AutoSaveRoundNumber(number,X)
{
var number2;
var TmpNum;
X=(!X ? 1:X);
number2 = Math.round(number*Math.pow(10,X))/Math.pow(10,X);
TmpNum = "" + number2;
var TmpArray = TmpNum.split(".");
if (TmpArray.length <2) { number2 = number2 + ".0"; }
return number2;
}
ASKER
Im coding this manually at this stage. Unforunately I dont have access to the IDE at this point.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. that worked.
<body onLoad="test();">
Bob