Go Premium for a chance to win a PS4. Enter to Win


Method to autosave a form at regular intervals

Posted on 2007-11-15
Medium Priority
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="http://www.w3.org/1999/xhtml" >
<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
  • 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
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

772 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