|
New to the CF scene
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
Ajax not returning data asynchronously
I am calling .ashx file from a javascript function.
The javascript functions that call .ashx file. LoopthroughRecordset() is called on a button click event.
var isResetLooping = false;
var refreshIntervalId;
function reload() {
$.ajax({
type: "POST",
url: "Handler.ashx",
data: { action: 'status', key: $('#hdfKey').val() },
dataType: "json",
success: function(response) {
if (response.Status1 != '') {
$('#txtStatus1').val(response.Status1);
}
if (response.Status2 != '') {
$('#txtStatus2').val(response.Status2);
}
if (response.Status3 != '') {
$('#txtStatus3').val(response.Status3);
}
if (response.Status4 != '') {
$('#txtStatus4').val(response.Status4);
}
if (response.Status5 != '') {
$('#txtStatus5').val(response.Status5);
}
if (response.Status6 != '') {
$('#txtStatus6').val(response.Status6);
}
if (response.Status7 != '') {
$('#txtStatus7').val(response.Status7);
}
if (response.Status8 != '') {
$('#txtStatus8').val(response.Status8);
}
//setTimeout(reload, 1000); //now that the request is complete, do it again in 1 second
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function LoopthroughRecordset() {
isResetLooping = false;
$('input[id="btnLoopRecordSet"]').attr('disabled', 'disabled');
$.ajax({
type: "POST",
url: "Handler.ashx",
data: { action: 'loop', key: $('#hdfKey').val() },
dataType: "json",
success: function(response) {
$('input[id="btnLoopRecordSet"]').removeAttr('disabled');
},
error: function(xhr, ajaxOptions, thrownError) {
$('input[id="btnLoopRecordSet"]').removeAttr('disabled');
}
});
refreshIntervalId = setInterval(reload, 1000);
return false;
}
Below is the .ashx file:
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.Web.SessionState
Public Class Handler : Implements IHttpHandler, IRequiresSessionState
Private currentContext As HttpContext
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "application/json"
currentContext = context
Dim action As String = context.Request.Form("action")
Dim key As String = context.Request.Form("key")
Dim result As String = Boolean.FalseString
Select Case action
Case "loop"
currentContext.Cache.Remove(key & "-Status1")
currentContext.Cache.Remove(key & "-Status2")
currentContext.Cache.Remove(key & "-Status3")
currentContext.Cache.Remove(key & "-Status4")
currentContext.Cache.Remove(key & "-Status5")
currentContext.Cache.Remove(key & "-Status6")
currentContext.Cache.Remove(key & "-Status7")
currentContext.Cache.Remove(key & "-Status8")
currentContext.Cache.Remove(key & "-reset")
LooptthroughRecordset(key)
Case "status"
context.Response.Write(RefreshStatus(key))
Case "reset"
SetStatusValue(key, "reset", "true")
Case "buttonHandler"
SetStatusValue(key, "buttonID", context.Request.Form("buttonId"))
SetStatusValue(key, context.Request.Form("textboxId"), context.Request.Form("textboxValue"))
End Select
End Sub
Private Function IsReset(ByVal key As String) As Boolean
Dim resetStatus As String = GetStatusValue(key, "reset")
If Not String.IsNullOrEmpty(resetStatus) Then
Return Convert.ToBoolean(resetStatus)
End If
Return False
End Function
Private Function tblLastNameUsedAlready(ByVal fldLastName As String) As Boolean
Dim rst As ADODB.Recordset
Dim strSQL As String = "SELECT * FROM tblLastName where fldLastName = '" & fldLastName & "'"
rst = DBO.ExecuteRecordset(strSQL)
If rst.EOF = True Then
rst.AddNew()
rst("fldLastName").Value = fldLastName
rst.Update()
tblLastNameUsedAlready = False
Else
tblLastNameUsedAlready = True
End If
rst.Close()
rst = Nothing
End Function
Private Sub tblNamesAdd5000Names(ByVal key As String)
Dim lng1 As Long, lng2 As Long
If IsReset(key) Then
Exit Sub
End If
SetStatusValue(key, "Status2", "tblNamesAdd5000Names")
lng1 = DCount("tblNames")
If lng1 > 2000 Then
Exit Sub
End If
Dim lngRecCount As Long
Randomize(Timer)
Do
If IsReset(key) Then
Exit Sub
End If
'create word
Dim strAccum As String
strAccum = ""
lng2 = 0
Do
If IsReset(key) Then
Exit Sub
End If
Dim lngAlpha As Long
lngAlpha = Int((122) * Rnd() + 1)
Do
If IsReset(key) Then
Exit Sub
End If
If lngAlpha >= 97 And lngAlpha <= 122 Then
Else
Exit Do
End If
strAccum = strAccum + LCase(Chr(lngAlpha))
lng2 = lng2 + 1
Loop Until 0 = 0
Loop Until lng2 = 8
tblNamesAddWord(key, strAccum)
lngRecCount = DCount("tblNames")
SetStatusValue(key, "Status4", "tblNamesAdd5000Names: " & Trim(Str(lngRecCount)))
Loop Until lngRecCount > 2000
End Sub
Private Sub tblNamesAddWord(ByVal key As String, ByVal fldLastName As String)
If IsReset(key) Then
Exit Sub
End If
SetStatusValue(key, "Status2", "tblNamesAddWord")
Dim rst As ADODB.Recordset
Dim strSQL As String = "SELECT * FROM tblNames where fldLastName = '" & fldLastName & "'"
rst = DBO.ExecuteRecordset(strSQL)
If rst.EOF = True Then
rst.AddNew()
rst("fldLastName").Value = fldLastName
rst.Update()
End If
rst.Close()
rst = Nothing
End Sub
Private Sub SetStatusValue(ByVal key As String, ByVal status As String, ByVal statusValue As String)
If currentContext.Cache(key & "-" & status) Is Nothing Then
currentContext.Cache.Add(key & "-" & status, statusValue, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, New TimeSpan(0, 20, 0), CacheItemPriority.Default, Nothing)
Else
currentContext.Cache(key & "-" & status) = statusValue
End If
End Sub
Private Function GetStatusValue(ByVal key As String, ByVal status As String) As String
If Not currentContext.Cache(key & "-" & status) Is Nothing Then
Return currentContext.Cache(key & "-" & status)
End If
Return String.Empty
End Function
Public Function RefreshStatus(ByVal key As String) As String
Dim s1, s2, s3, s4, s5, s6, s7, s8 As String
s1 = GetStatusValue(key, "Status1")
s2 = GetStatusValue(key, "Status2")
s3 = GetStatusValue(key, "Status3")
s4 = GetStatusValue(key, "Status4")
s5 = GetStatusValue(key, "Status5")
s6 = GetStatusValue(key, "Status6")
s7 = GetStatusValue(key, "Status7")
s8 = GetStatusValue(key, "Status8")
Return "{""Status1"":""" & GetStatusValue(key, "Status1") & """," & _
"""Status2"":""" & GetStatusValue(key, "Status2") & """," & _
"""Status3"":""" & GetStatusValue(key, "Status3") & """," & _
"""Status4"":""" & GetStatusValue(key, "Status4") & """," & _
"""Status5"":""" & GetStatusValue(key, "Status5") & """," & _
"""Status6"":""" & GetStatusValue(key, "Status6") & """," & _
"""Status7"":""" & GetStatusValue(key, "Status7") & """," & _
"""Status8"":""" & GetStatusValue(key, "Status8") & """}"
End Function
Public Sub LooptthroughRecordset(ByVal key As String)
Dim rst As ADODB.Recordset
Try
SetStatusValue(key, "Status1", "start")
SetStatusValue(key, "Status2", "LooptthroughRecordset")
tblNamesAdd5000Names(key)
Dim s1, s2, s3, s4, s5, s6, s7, s8 As String
s1 = GetStatusValue(key, "Status1")
s2 = GetStatusValue(key, "Status2")
Dim lngCurRecCount As Long, strDisplay As String, lngMaxRecs As Long
Dim fldLastName As String
If IsReset(key) Then
Exit Sub
End If
Dim strSQL As String = "SELECT * FROM tblNames ORDER BY fldID ASC"
rst = DBO.ExecuteRecordset(strSQL)
If rst.EOF = False Then
lngMaxRecs = rst.RecordCount
Do
Select Case GetStatusValue(key, "buttonID")
Case "Button1"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus1")
Case "Button2"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus2")
Case "Button3"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus3")
Case "Button4"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus4")
Case "Button5"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus5")
Case "Button6"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus6")
Case "Button7"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus7")
Case "Button8"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus8")
Case "Button9"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus9")
Case "Button10"
Dim str1 As String
str1 = GetStatusValue(key, "txtStatus10")
End Select
currentContext.Cache.Remove(key & "-buttonID")
If IsReset(key) Then
Exit Sub
End If
lngCurRecCount = lngCurRecCount + 1
strDisplay = "tblNames: " + Trim(Str(lngCurRecCount)) + " of " + Trim(Str(lngMaxRecs))
SetStatusValue(key, "Status3", strDisplay)
Do
If IsReset(key) Then
Exit Sub
End If
If tblLastNameUsedAlready(fldLastName) = True Then
Exit Do
End If
If fldLastName <> "" Then
fldLastName = CapiitalizeWord(key, fldLastName)
rst("fldLastName").Value = fldLastName
End If
Loop Until 0 = 0
rst.Update()
s3 = GetStatusValue(key, "Status3")
rst.MoveNext()
Loop Until rst.EOF = True
End If
SetStatusValue(key, "Status1", "done")
Finally
If Not rst Is Nothing Then
rst.Close()
rst = Nothing
End If
End Try
End Sub
Private Function CapiitalizeWord(ByVal key As String, ByVal fldWord As String) As String
Dim str1 As String, str2 As String, str3 As String
SetStatusValue(key, "Status2", "CapiitalizeWord")
str1 = fldWord
str2 = UCase$(Left$(str1, 1))
str3 = Mid$(str1, 2)
CapiitalizeWord = str2 + str3
End Function
Private Function DCount(ByVal strTblName As String) As Integer
Dim count As Integer = 0
Dim rst As ADODB.Recordset
Dim strSQL As String = "SELECT * FROM tblNames ORDER BY fldID ASC"
rst = DBO.ExecuteRecordset(strSQL)
count = rst.RecordCount
rst.Close()
rst = Nothing
Return count
End Function
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
The problem is below:
Textboxes should display the looping through the recordset asynchronously.
When I debug the single form the it works fine. The loop through the recordset displays in textboxes(Status1, Status2...so on) asynchronously. But when I call it from other page then it displays the complete data in textboxes after looping through the recordset. It loops through the recordset and but not update textboxes asynchronously.
|
|