Click here to Skip to main content
15,919,423 members
Articles / Web Development / ASP.NET

Easy Automatically Save Form with .NET and AJAX

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
25 Mar 2009CPOL2 min read 57.4K   763   24   11
How to save a form automatically in the background


This will show how to auto save a web form in the background without affecting user input.


I was working with a large form and was looking for an easy way to do an auto save so that the data would not get lost if the session timed out or for some other reason when the user did not click save button. I found some examples which were not so easy to implement. I just wanted to do the same function as when clicking the save button. I then tried some ways to do this with the AJAX controls and found it to be super easy.

Using the Code

The code in this article is simple by just moving the text form one textbox to another. But this can be replaced to do the actual saving of the form content (for example, to a database).

To do this, first create a form and your form fields. Then add a ScriptManager, Timer and an UpdatePanel from AJAX Extensions tab in the Toolbox.

If you do not want anything to be displayed when saving, leave the UpdatePanel empty.

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="15000" ontick="Timer1_Tick"></asp:Timer> 


    <asp:TextBox ID="TextBox1" runat="server" 
	Height="118px" TextMode="MultiLine" Width="468px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Save" 
	onclick="Button1_Click" /><br /><br />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

        <asp:TextBox ID="TextBox2" runat="server" Height="120px" 
		TextMode="MultiLine" Width="466px"></asp:TextBox>

        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>




In the code behind, the save button click event calls the save method which in this case only copies the text between the textboxes:

protected void Button1_Click(object sender, EventArgs e)

private void Save()
    //Add the save function here ex store the text to DB
    //Here we only move between the two textboxes to show that it works
    TextBox2.Text = TextBox1.Text;

The only thing now is to add a handler for the timer onTick event and make that call the same Save function.

protected void Timer1_Tick(object sender, EventArgs e)

Now it is done!

Points of Interest

The key thing here is the AsyncPostBackTrigger. Without the trigger, the form will do a postback and the form will flicker for the user and lose focus on the current form field (and you don't want that to happen on a time interval when you write a long text).


  • 20th March, 2009: Initial post
  • 25th March, 2009: Made some minor text changes


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Sigma IT & Management
Sweden Sweden
Working with web application development since 1999.
Developer of the CMS product Publech ( and a large range of other Publech modules.

Involved in developing the largest non commercial website in Sweden the Swedish Public Employment Service (

Comments and Discussions

QuestionHow can one avoid losing focus if the enduser is typing when the Tick occurs? Pin
mkamoski5-Jun-18 5:30
mkamoski5-Jun-18 5:30 
GeneralThanks! Pin
Jonathan Hankey26-Jan-11 9:18
Jonathan Hankey26-Jan-11 9:18 
GeneralRe: Thanks! Pin
Mr Orange21-Aug-12 4:27
Mr Orange21-Aug-12 4:27 
Generalvery good article Pin
Donsw10-Apr-09 7:37
Donsw10-Apr-09 7:37 
GeneralCool, but kind of hard to read Pin
GMorris25-Mar-09 3:25
GMorris25-Mar-09 3:25 
I had a little trouble trying to figure out what was supposed to be said sometimes, like "Not it is done!", which I assume to be "Now it is done!". It takes me a minute to figure it out, especially if there are multitudes of mistakes like that, though the article is informative. I make typos from time to time, but I generally do a good proofreading before I actually commit the post. Also, there are two words, "lose" and "loose" and both have very different meanings. That throws me off as well, and I have to spend extra time trying to determine what is meant. Sorry for the criticism, it's just hard to read like that.

Gary W. Morris, Entepreneur

GeneralRe: Cool, but kind of hard to read Pin
Mr Orange25-Mar-09 4:04
Mr Orange25-Mar-09 4:04 
Generalnice Pin
Mohm'ed Melhem25-Mar-09 1:38
professionalMohm'ed Melhem25-Mar-09 1:38 
GeneralVery Interesting Pin
samerh23-Mar-09 21:24
samerh23-Mar-09 21:24 
GeneralRe: Very Interesting Pin
Mr Orange23-Mar-09 22:05
Mr Orange23-Mar-09 22:05 
GeneralRe: Very Interesting Pin
Mohm'ed Melhem25-Mar-09 1:42
professionalMohm'ed Melhem25-Mar-09 1:42 
GeneralRe: Very Interesting Pin
rahul-pawar16-Jul-10 16:12
rahul-pawar16-Jul-10 16:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.