Solved

Method to autosave a form at regular intervals

Posted on 2007-11-15
4
399 Views
Last Modified: 2007-11-26
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.


<%@ 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;

  }

Open in new window

0
Comment
Question by:jcwalker
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Try this on the HTML designer

<body onLoad="test();">

Bob
0
 

Author Comment

by:jcwalker
Comment Utility
Im coding this manually at this stage.  Unforunately I dont have access to the IDE at this point.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 400 total points
Comment Utility
I am talking about right in your file, in the HTML portion:

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

Bob
0
 

Author Comment

by:jcwalker
Comment Utility
Thanks.  that worked.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now