如何实现“自动保存"?或“保存草稿"; ASP.NET中的功能? [英] How to implement an "Auto Save" or "Save Draft" feature in ASP.NET?

查看:93
本文介绍了如何实现“自动保存"?或“保存草稿"; ASP.NET中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET 2.0中有一个注册表.我想通过单击提交"按钮来保存我的注册表单字段,或者应该每五秒钟保存一次.

I have a registration form in ASP.NET 2.0. I want to save my registration form fields either by clicking on submit button or they should be saved every five seconds.

例如,我在注册页面上有三个字段:

For example I have three fields in my registration page:

UID PWD Name

用户已输入UIDPWD,并且在输入Name时应保存以前的值,而不会中断用户输入

The user has entered UID and PWD and whilst he is entering Name the previous values should be saved without interruption of user inputs

我将如何在ASP.NET中做到这一点?

How would I do this in ASP.NET?

推荐答案

您可以使用Javascript& jQuery的.具有由计时器触发的功能,该功能定期读取要保存的表单数据并将其发布回SaveDraft.aspx页面.在此页面中,将数据保留在某处(例如数据库).

You could do this with a snippet of Javascript & jQuery. Have a function that's fired by a timer that periodically reads the form data you want to save and posts it back to a SaveDraft.aspx page. In this page persists the data somewhere (such as a database).

如果用户注销或会话丢失,则可以查询此数据,并在该数据存在的情况下预先填充表格.

If the user logs out or their session is lost you can query for this data and pre-populate the form if the data exists.

在数据输入ASPX页面上:

On your data entry ASPX page:

// Usual ASP.NET page directives go here

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js" ></script>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        <asp:textbox id="username" runat="server" /><br />
        <asp:textbox id="password" runat="server" /><br />
        <asp:textbox id="realName" runat="server" /><br />
        <asp:button id="Submit" onclick="Submit_Click" 
              usesubmitbehavior="true" runat="server" />
      </div>
    </form>

    <script type="text/javascript">
      $(document).ready(function () {
        // Configure to save every 5 seconds
        window.setInterval(saveDraft, 5000);
      });

      // The magic happens here...
      function saveDraft() {
        $.ajax({
          type: "POST",
          url: "SaveDraft.aspx",
          data: ({
            username: $("#<%=username.ClientID %>").val(),
            password: $("#<%=password.ClientID %>").val(),
            realName: $("#<%=realName.ClientID %>").val()
          }),
          success: function (response) {
            alert('saved draft');
          }
        });
      }
    </script>
  </body>
</html>

在您的SaveDraft.aspx页面中:

public partial class SaveDraft : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string username = Request.Form["username"];
    string password = Request.Form["password"];
    string realName = Request.Form["realName"];

    // Save data somewhere at this point    
  }
}

那应该让您入门.

这篇关于如何实现“自动保存"?或“保存草稿"; ASP.NET中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆