Click here to Skip to main content
11,933,213 members (63,317 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Automatically Submit Form Data

, 17 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Automatically submit form data using ASP.NET and JavaScript.


In this article, I explain how to save user input data automatically when filling forms. This functionality is similar to ASP.NET profiling. This is useful for large web forms and if the user closes the browser without saving the form. Here, I also use ASP.NET caching to retrieve data for authenticated users.


JavaScript is used to bind events on the client side, collect user input data, and also to fill a hash table with user input. On a given period, the hash table is serialized and sent as a query string to the ASPX page using an XmlHttp object. AutoSave.aspx populates an in-memory object with a query string value and on session timeout, values are saved to the database.

Using the code

  1. Bind the OnBlur event of all input controls to fill a hash table when user inputs are given.
  2. Submit all user input data to the server side.
  3. Submit all data to the server side just before the user closes the browser.
<script src="jshashtable.js" type="text/javascript"></script>

The server-side code:


protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
        UserData userData;
        if (Cache[Context.User.Identity.Name] == null)
            userData = new UserData();
            Cache.Insert(Context.User.Identity.Name, userData,
              null, Cache.NoAbsoluteExpiration,

            CacheItemPriority.Default, new CacheItemRemovedCallback(CacheExpired));
            userData = Cache[Context.User.Identity.Name] as UserData;
        FillPage(userData); //to populate web form controls with values from DTO

internal void CacheExpired(string key, object val, CacheItemRemovedReason reason)
    if (reason != CacheItemRemovedReason.Removed)
        //Save.aspx invokes userData.updateDB() to update data in database
        HttpContext.Current.Server.Execute("Save.aspx", new StringWriter());

public void FillPage(UserData userData)
    //Add code here..


protected void Page_Load(object sender, EventArgs e)
    UserData userData = Cache[Context.User.Identity.Name] as UserData;
    for (int i = 0; i < Request.QueryString.Count; i++)
            userData[Request.QueryString.GetKey(i)] = Request.QueryString.Get(i);
        catch (Exception ex)

    Cache[Context.User.Identity.Name] = userData;


public class UserData
    public UserData()
        // TODO: Add constructor logic here

    public string this[string paramName]
    {// string indexer
            return this.GetType().GetProperty(paramName).GetValue(this, null).ToString();
            this.GetType().GetProperty(paramName).SetValue(this, value, null);
    private string _LastName;
    public string LastName
        get { return _LastName; }
        set { _LastName = value; }

    private string _FirstName;
    public string FirstName
        get { return _FirstName; }
        set { _FirstName = value; }

    private string _Email;
    public string Email
        get { return _Email; }
        set { _Email = value; }

    public void updateDB()

        /***here: invoke Stored Procedure to update data in database***/




  1. If the page form is automatically filled by a web-form-filler software, this won't work.
  2. In modern browsers, query string support is only up to 2000 chars; data loss happens if that length is exceeded.



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


About the Author

Md.Asaduzzaman Azad
Data Path Limited
Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood one Pin
Md. Marufuzzaman26-May-09 19:24
memberMd. Marufuzzaman26-May-09 19:24 
This is Gr8 & It will might be help the web developers.

Md. Marufuzzaman

GeneralRe: Good one Pin
Md. Marufuzzaman27-Aug-09 2:27
groupMd. Marufuzzaman27-Aug-09 2:27 
GeneralClosing the Browser Pin
stixoffire22-May-09 4:43
memberstixoffire22-May-09 4:43 
GeneralNice Attempt Pin
m_irfan20-May-09 10:16
memberm_irfan20-May-09 10:16 
GeneralNice work Pin
tareq0719-May-09 19:49
membertareq0719-May-09 19:49 
GeneralMy vote of 1 Pin
robvon18-May-09 17:35
memberrobvon18-May-09 17:35 
GeneralRe: My vote of 1 Pin
Dewey19-May-09 0:11
memberDewey19-May-09 0:11 
GeneralVery Nice Pin
JeffCirceo18-May-09 9:49
memberJeffCirceo18-May-09 9:49 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 17 May 2009
Article Copyright 2009 by Md.Asaduzzaman Azad
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid