Method to autosave a form at regular intervals

Posted on 2007-11-15
Last Modified: 2007-11-26

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"
<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
		'Load Default Note
		txtNoteText.InnerText= ReadNoteFromFile("TODO")
		End If
	End Sub
	Protected Sub ddlNotes_SelectedIndexChanged(ByVal sender As Object, ByVal e As 
		' 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 = ""
			txtNoteText.InnerText = ReadNoteFromFile(ddlNotes.SelectedValue)
		End If
	End Sub
	Protected Sub btnDeleteNote_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		' Reload the note list and set to the default selection
		' since the current note no longer exists.
	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.
	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.
		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 & 
		' Write text
		' Flush to disk and close file
	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()
		' 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
<html xmlns="" >
<script language=JavaScript src=autosave.js></script>
<script language=JavaScript>
	function test()
<form id="myForm" runat="server">
	<asp:DropDownList ID="ddlNotes" runat="server"
	<asp:Button ID="btnDeleteNote" Text="Delete Note" runat="server"
	<br /><br />
	Note Name: <asp:TextBox ID="txtNoteName" width="255" runat="server" />
	<asp:Button ID="btnSaveNote"  Text="Save Note" runat="server"
	/><br />
	<TextArea id="txtNoteText" cols="80" rows="30" runat="server" />
Here is the javascript function I found:
  var mnAutoSaveMilliSeconds=0;
  var mnAutoSaveMilliSecondsExp=0;
  var mnAutoSaveInterval=30000;
  function AutoSaveInit(nMilliSeconds)
       var nMinutes=0;
       nMinutes = ((nMilliSeconds / 1000) / 60); 
       mnAutoSaveMilliSeconds = nMilliSeconds; 
       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()
    catch (exception) { }
  function AutoSaveSubmit()
         Call the form submittal code in your main page.
    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;

Open in new window

Question by:jcwalker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 20298156
Try this on the HTML designer

<body onLoad="test();">


Author Comment

ID: 20318913
Im coding this manually at this stage.  Unforunately I dont have access to the IDE at this point.
LVL 96

Accepted Solution

Bob Learned earned 400 total points
ID: 20320155
I am talking about right in your file, in the HTML portion:

<body onLoad="test();">
<form id="myForm" runat="server">


Author Comment

ID: 20354332
Thanks.  that worked.

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question