7

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

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

How would I do this in ASP.NET?

1 Answer 1

9

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.

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>

In your SaveDraft.aspx page:

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

That should get you started.

Sign up to request clarification or add additional context in comments.

8 Comments

can you post the function and timer call too?
I got your point. But i dont know how to do that? so if u post source code that will be easy for me.
What is the equivalent of Response.Form["username"]; in Vb .net?
@Dogahe Response.Form("username") - VB uses parenthesis (round brackets) instead of square brackets.
@Kev When I did Response.Form("username") I get the error: 'Form' is not a member of 'System.Web.HttpResponse'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.