About the author

Vijay Kodali
E-mail me Send mail

Site Statistics

Site Meter

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Auto save Asp.net form values Asynchronously

In this article, I will explain how to save Asp.Net page values asynchronously (aka Gmail style of saving mail drafts).  

Introduction:

In the past, Web applications were known for having less usable, less responsive user interfaces. AJAX changed all of that. The application can work asynchronously and the user doesn't have to sit and wait for pages to refresh. :

What is Ajax?

Ajax (Asynchronous JavaScript and XML) is an approach to web development that uses client-side scripting to exchange data with a web server. 

Solution:

There are several ways of achieve it. In this article I am using AJAX functionality to call ASP.NET Web services from the browser by using client script. Yes, no updatepanel.  

Start by adding a web service to the project as shown below, name it as AsynchronousSave.asmx. Make this web service accessible from Script, by setting class qualified with the ScriptServiceAttribute attribute...  

[System.Web.Script.Services.ScriptService]    
public class AsynchronousSave : System.Web.Services.WebService 


Here is the AsynchronousSave webservice class: 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {

        return "Hello World";

    }

    [WebMethod]
    public string SaveInput(String input)
    {
        string StrInput = Server.HtmlEncode(input); if (!String.IsNullOrEmpty(StrInput))
        {

            string[] strFields = StrInput.Split('+');

            //code to save all input values
            // you can easily savethese values to temp DB table, temp file or xml file
            //Dispaly last saved values

            return String.Format("Last saved text: FirstName {0} ,<br/> Last name {1} <br/> Last "

            + "saved draft {2} at {3}.", strFields[0], strFields[1], strFields[2], DateTime.Now);

        }

        else
        {

            return ""; //if input values are empty..retrun empty string
        }
    }
}
 
Nothing fancy here, just methods marked with [WebMethod] attribute. The Saveinput method takes an input string with “+” as delimiter between form values.  

To enable web service to be called from client side, add script manager to page  

<asp:ScriptManager runat="server" ID="scriptManager">
    <Services>
        <asp:ServiceReference Path="~/AsynchronousSave.asmx" />
    </Services>
</asp:ScriptManager>


And here is HTML of page

<div>
    First Name:<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></br> 
    LastName:<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></br> 
    Draft :<asp:TextBox ID="txtDraft" runat="server"></asp:TextBox></br></div>
<div id="Preview">

The following example shows the java script that makes service calls

<script language ="javascript" >
    //This function calls the web service    
    function SaveUserInput() {
        var fName = document.getElementById("txtFirstName");
        var lName = document.getElementById("txtLastName");
        var draft = document.getElementById("txtDraft");
        //Saving all input in a single value         
        var input = fName.value + "+" + lName.value + "+" + draft.value;
        //ProjName.WebServiceName.WebMethod         
        SampleApplication1.AsynchronousSave.SaveInput(input, SucceededCallback);
    }
    // This is the callback function that processes the Web Service return value.     
    function SucceededCallback(result) {
        var divPreview = document.getElementById("Preview");
        divPreview.innerHTML = result;
    }
    // execute SaveUserInput for every 10 sec, timeout value is in miliseconds
    window.setInterval('SaveUserInput()', 10000); 
    
</script>

Reference:

http://msdn.microsoft.com/en-us/library/bb398998.aspx

         


Tags:
Posted by vijay on Monday, November 23, 2009 2:17 PM
Permalink | Comments (6) | Post RSSRSS comment feed

Comments

movie quotations United States

Friday, August 27, 2010 10:08 PM

movie quotations

to be honest, some of the people i know, still quote alot of older and more famous movies, sometimes it does get annoying but you know, i eitger get used to it or i ignore them.

provestra United States

Sunday, August 29, 2010 1:15 PM

provestra

She is just extraordinary! All she touches spin to gold, so regardless of some rumors or lies about her, she stays on the top. Way to go girl, we love you

stop smoking hypnosis United States

Monday, August 30, 2010 3:13 AM

stop smoking hypnosis

Another method for you is to stay in a house that is free from other smokers. Having to live with other smokers is such a challenge because you will be constantly tempted by the smell and by the cigarettes lying around that you can easily get.

garmin 255w United States

Tuesday, August 31, 2010 12:07 AM

garmin 255w

Good day, I'm a college English major and I'm learning a lot about writing by reading blogs. I really like your style of writing. It's very easy to understand but with good details. Your vocabulary makes it easy to read and understand. That's a big segment of writing. Your followers have to be able to understand what you're saying and the article has to be exciting. You need to engage your readers, so they will come back for more. You do a fantastic job with all of these aspects. Thanks!

Virility Ex United States

Thursday, September 02, 2010 11:25 PM

Virility Ex

She is just fantastic! All she touches it turns to gold, so despite some chitchats or fabrications about her, she remains on the top. So go girl, you're the best

Virility Ex United States

Friday, September 03, 2010 7:29 AM

Virility Ex

She is just fantastic! Everything she touches spin to gold, so no matter some gossips or fabrications about her, she remains on the top. Way to go girl, the sky is your limit.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading