代码之家  ›  专栏  ›  技术社区  ›  Sandy

如何在ASP.NET中实现“自动保存”或“保存草稿”功能?

  •  7
  • Sandy  · 技术社区  · 15 年前

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

    例如,我的注册页面中有三个字段:

    UID PWD Name

    用户已输入 UID 随钻测井 当他进入的时候 名字 应保存以前的值,而不中断用户输入。

    如何在ASP.NET中执行此操作?

    1 回复  |  直到 11 年前
        1
  •  9
  •   erlando    13 年前

    SaveDraft.aspx

    // 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>
    

    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    
      }
    }