0

如何在用户注销之前每 10 秒自动保存一次 Web 表单。有没有使用 system.thread.sleep 类的方法。?或者有没有其他可行的方法来实现这一点。我正在使用.net 2.0。提前致谢。

4

3 回答 3

1

使用计时器并在您要保存的页面上,并设置 10 秒的间隔。并将所有表单数据保存在计时器的滴答事件中。所以 tick 事件将每 10 秒后执行一次,您的表单将被保存。

请参阅下面的链接以获取与计时器滴答事件相关的示例以执行某些任务

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

于 2012-10-23T07:25:43.910 回答
1

作为帮助您继续前进的提示,这里使用 jQuery 是一些代码:

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

于 2012-10-23T07:53:15.710 回答
0

最好的方法是使用定时器控件,它们会触发滴答事件,您可以在其上编写代码来保存数据,将定时器时间设置为 10000。

于 2012-10-23T07:50:37.440 回答