I have created a online_test_paper.aspx and in this page one question show at a time along with their objective answers. I want to save all answers automatically, afrter a time period so that dis-connectivity of the web can't waste our time and effort to
solve it again thanks in advanced...
1 Create Web Service with this name AsynchronousSave
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AutoSave;
/// <summary>
/// Summary description for AsynchronousSave
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AsynchronousSave : System.Web.Services.WebService {
MyClass objMyclass = new MyClass();
public AsynchronousSave () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[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
if (strFields[1].Equals(""))
{
return "Second Filed Are Empty";
}
else
{
objMyclass.fname = strFields[0];
objMyclass.Lname = strFields[1];
objMyclass.Draft = strFields[2];
string Description=string.Empty;
InserData.InsertRecord(objMyclass, ref Description);
//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);
if (Description.Equals("SUCCESS"))
{
return String.Format("Last Record Saved on " + DateTime.Now);
}
else
{
return String.Format("Last Error on Save Record " + DateTime.Now);
}
}
}
else
{
return ""; //if input values are empty..retrun empty string
}
}
public class MyClass
{
public string fname { get; set; }
public string Lname { get; set; }
public string Draft { get; set; }
}
}
2 Then Create a Class with the name of InserData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoSave;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for InserData
/// </summary>
///
namespace AutoSave
{
public class InserData
{
public InserData()
{
//
// TODO: Add constructor logic here
//
}
Para[0] = new SqlParameter("@fname", objGstore.fname);
Para[1] = new SqlParameter("@Lname", objGstore.Lname);
Para[2] = new SqlParameter("@Draft", objGstore.Draft);
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
//This function calls the web service
// var Inter;
function SaveUserInput() {
var reason = "";
var fName = document.getElementById('<%=txtFirstName.ClientID%>');
var lName = document.getElementById('<%=txtLastName.ClientID%>');
var draft = document.getElementById('<%=txtDraft.ClientID%>');
}
// This is the callback function that processes the Web Service return value.
function SucceededCallback(result) {
var divPreview = document.getElementById("Preview");
divPreview.innerHTML = result;
var draft = document.getElementById('<%=txtDraft.ClientID%>');
draft.value = 'Done';
// clearInterval(Inter); // this Function Dispose The Interval Function
}
// execute SaveUserInput for every 10 sec, timeout value is in miliseconds
//window.setInterval('SaveUserInput()', 10000);
function CallAutoSave() {
Inter = window.setInterval('SaveUserInput()', 10000);
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<br />
<div>
First Name:<asp:TextBox ID="txtFirstName" runat="server" onblur="CallAutoSave()"></asp:TextBox><br />
umeshjha.net
Member
21 Points
50 Posts
auto save after few minutes
Jan 29, 2009 12:33 PM|LINK
hello sir/mam
I have created a online_test_paper.aspx and in this page one question show at a time along with their objective answers. I want to save all answers automatically, afrter a time period so that dis-connectivity of the web can't waste our time and effort to solve it again thanks in advanced...
MetalAsp.Net
All-Star
112151 Points
18246 Posts
Moderator
Re: auto save after few minutes
Jan 29, 2009 01:12 PM|LINK
Would the Timer control work for you?
umeshjha.net
Member
21 Points
50 Posts
Re: auto save after few minutes
Jan 30, 2009 03:54 AM|LINK
No But it is ajax enabled site so we can use timer control
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: auto save after few minutes
Aug 02, 2012 05:40 PM|LINK
There is Some Step so please follow them
you can also download this example code from my skyDrive
https://skydrive.live.com/?cid=2F22272220E37707&id=2F22272220E37707%21103
1 Create Web Service with this name AsynchronousSave
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AutoSave;
/// <summary>
/// Summary description for AsynchronousSave
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AsynchronousSave : System.Web.Services.WebService {
MyClass objMyclass = new MyClass();
public AsynchronousSave () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[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
if (strFields[1].Equals(""))
{
return "Second Filed Are Empty";
}
else
{
objMyclass.fname = strFields[0];
objMyclass.Lname = strFields[1];
objMyclass.Draft = strFields[2];
string Description=string.Empty;
InserData.InsertRecord(objMyclass, ref Description);
//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);
if (Description.Equals("SUCCESS"))
{
return String.Format("Last Record Saved on " + DateTime.Now);
}
else
{
return String.Format("Last Error on Save Record " + DateTime.Now);
}
}
}
else
{
return ""; //if input values are empty..retrun empty string
}
}
public class MyClass
{
public string fname { get; set; }
public string Lname { get; set; }
public string Draft { get; set; }
}
}
2 Then Create a Class with the name of InserData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoSave;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
/// <summary>
/// Summary description for InserData
/// </summary>
///
namespace AutoSave
{
public class InserData
{
public InserData()
{
//
// TODO: Add constructor logic here
//
}
public static void InsertRecord(object DeleteCartItem, ref string Description)
{
try
{
MyClass objGstore = ((MyClass)DeleteCartItem);
SqlParameter[] Para = new SqlParameter[4];
Para[0] = new SqlParameter("@fname", objGstore.fname);
Para[1] = new SqlParameter("@Lname", objGstore.Lname);
Para[2] = new SqlParameter("@Draft", objGstore.Draft);
Para[3] = new SqlParameter("@retUserMessage", SqlDbType.VarChar);
Para[3].Size = 250;
Para[3].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(DAL.DBConnection(), CommandType.StoredProcedure, "AddRecord", Para);
Description = Para[3].Value.ToString().Trim();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
3 Then Refenrnce you web service in ScriptManger
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/AsynchronousSave.asmx" />
</Services>
</asp:ScriptManager>
4 Now Apply this Code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
//This function calls the web service
// var Inter;
function SaveUserInput() {
var reason = "";
var fName = document.getElementById('<%=txtFirstName.ClientID%>');
var lName = document.getElementById('<%=txtLastName.ClientID%>');
var draft = document.getElementById('<%=txtDraft.ClientID%>');
if (Page_ClientValidate()) {
var input = fName.value + "+" + lName.value + "+" + draft.value;
//ProjName.WebServiceName.WebMethod
this.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;
var draft = document.getElementById('<%=txtDraft.ClientID%>');
draft.value = 'Done';
// clearInterval(Inter); // this Function Dispose The Interval Function
}
// execute SaveUserInput for every 10 sec, timeout value is in miliseconds
//window.setInterval('SaveUserInput()', 10000);
function CallAutoSave() {
Inter = window.setInterval('SaveUserInput()', 10000);
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<br />
<div>
First Name:<asp:TextBox ID="txtFirstName" runat="server" onblur="CallAutoSave()"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqFname" runat="server" ErrorMessage="Required First Name" ControlToValidate="txtFirstName" ValidationGroup="Save"></asp:RequiredFieldValidator>
<br />
LastName:<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqLname" runat="server" ErrorMessage="Required Last Name" ControlToValidate="txtLastName" ValidationGroup="Save"></asp:RequiredFieldValidator>
<br />
Draft :<asp:TextBox ID="txtDraft" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqDraft" runat="server" ErrorMessage="Required Draft" ControlToValidate="txtDraft" ValidationGroup="Save"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSave" runat="server" Text="Auto Save" ValidationGroup="Save"/>
</div>
<div id="Preview">
</div>
</asp:Content>
Sohail Shaikh
chetan.sarod...
All-Star
65749 Points
11148 Posts
Re: auto save after few minutes
Aug 03, 2012 03:41 AM|LINK
Hi
You can do this by using the Ajax Timer control. Do the following :
Refer:
http://www.codeproject.com/KB/aspnet/AutoSaveFormData.aspx
http://forums.asp.net/p/1510020/3594968.aspx
http://forums.asp.net/p/1531233/3707371.aspx
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: auto save after few minutes
Aug 05, 2012 07:50 AM|LINK
But Timer run on server side and its make havy load to server when 50+ user login to you web and decrease your
server performance
you can download a sample project from my SkyDrive there is two autosave project please use this
shaikh sohail
Sohail Shaikh
burepallisri
Member
154 Points
161 Posts
Re: auto save after few minutes
Aug 05, 2012 02:45 PM|LINK
just maintain one timer for specific amount of time and then save the data.
you can do using javascript timer or timer control present in the asp.net...