shakz
shakz

Reputation: 639

Autosave a form till the session expires

How to autosave a web form in every 10 seconds till the user log out. Is there any method using system.thread.sleep class.? Or is there any other feasible method to achieve this. I am using .net 2.0. Thanks in advance.

Upvotes: 0

Views: 2408

Answers (3)

Aristos
Aristos

Reputation: 66641

As a hint to help you move on, and using jQuery here is some code:

var ajxAutoSaveTimer = null;

function TriggerChanges()
{
   if(ajxAutoSaveTimer)
     clearTimeout(ajxAutoSaveTimer);

   ajxAutoSaveTimer = setTimeout(MakeAutoSave, 10000);
}

function MakeAutoSave()
{
  // fix the data that you going to send here.

  jQuery.ajax({
        url: "PageName.aspx/MethodName",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(data) 
        {
          // you can make a tiny notification here
          return;
        }
        error: function(responseText, textStatus, XMLHttpRequest) {             
                return;
            }
    });
}

jQuery(document).ready(function() {
    jQuery(":input:not(*[ExcludeFromNotice])").bind('change', TriggerChanges);
});

Now what I do here. Starting with this line, I capture all the input that are the changes by the user controls. All exept the one that I have flagged with ExcludeFromNotice because some of them I do not won to be part of the autosave process.

jQuery(":input:not(*[ExcludeFromNotice])").bind('change', TriggerChanges);

you can set that to a control as "ExcludeFromNotice=true" on properties. Alternative you can set a class only to the controls you like to be part of the auto-save notification.

Now on TriggerChanges() what I do is to create a time that is fired after 10sec that user not make other changes.

And the rest part that is the most difficult, if the MakeAutoSave(), there you need to collect and post the autosaved data.

Some extra pages that can help you make the ajax call that saves the data:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
How to post form data in asp.net using ajax

Upvotes: 1

LearningAsp
LearningAsp

Reputation: 361

The best method is to use timers control ,they will fire tick events ,on which you can write code for saving your data ,set the timer time to 10000.

Upvotes: 0

RVD
RVD

Reputation: 66

use timer and on your page which you want to save, and set interval of 10 seconds. and save all form data on tick event of timer. so the tick event will be executed after every 10 seconds and your form will get saved.

Please refer link below for the example related to timer tick event to perform some task

http://www.devasp.net/net/articles/display/1412.html

Upvotes: 1

Related Questions