Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2009
Views 14,455
Downloads 221
Bookmarked 19 times

Easy Automatically Save Form with .NET and AJAX

By | 25 Mar 2009 | Article
How to save a form automatically in the background

Introduction

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

Background

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> 

<div> 

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

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

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

</asp:UpdatePanel> 

</div>

</form>       

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)
{
    Save();
}

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)
{
    Save();
}

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).

History

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

License

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

About the Author

Mr Orange

Software Developer (Senior)
Sigma IT & Management
Sweden Sweden

Member

Working with web application development since 1999.
Developer of the CMS product Publech (www.publech.com) and a large range of other Publech modules.
 
Involved in developing the largest non commercial website in Sweden the Swedish Public Employment Service (www.ams.se)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks! PinmemberJonathan Hankey9:18 26 Jan '11  
Generalvery good article PinmemberDonsw7:37 10 Apr '09  
GeneralCool, but kind of hard to read PinmemberGMorris3:25 25 Mar '09  
GeneralRe: Cool, but kind of hard to read PinmemberMr Orange4:04 25 Mar '09  
Generalnice PinmemberMohm'ed Melhem1:38 25 Mar '09  
GeneralVery Interesting Pinmembersamerh21:24 23 Mar '09  
GeneralRe: Very Interesting PinmemberMr Orange22:05 23 Mar '09  
GeneralRe: Very Interesting PinmemberMohm'ed Melhem1:42 25 Mar '09  
GeneralRe: Very Interesting Pinmemberrahul-pawar16:12 16 Jul '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Mr Orange
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid