ASP.Net - Ajax - Sample

Ajax was a technology added to ASP.Net Web Pages. Basicially it allowed you to add Javascript to your web page that would make a call to your server, to send and or retrieve data, without causing the web page to perform a postback.

JavaScript

This function is activated when I press the generate button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function GenerateHistoryInitialize(dbCode, tableName) {
var chkManageDdTable = $('#<%= chkManageDdTable.ClientID %>').is(':checked');
var ddlDatabaseType = $('#<%= ddlDatabaseType.ClientID %>').val();

var params = new Object();
params.dbCode=dbCode;
params.manageDdTable = chkManageDdTable;
params.tableName = tableName;
params.outputDatabaseType = ddlDatabaseType;

//alert (JSON.stringify(params));
$.ajax({
type: 'POST',
url: 'ajax.aspx/GenerateHistoryInitialize',
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
success: function (msg) {
//
// good. It succeeded
//
$('#txtBlocksDone').val(parseInt($('#txtBlocksDone').val()) + 1);
$('#<% =txtOutput.ClientID %>').val($('#<% =txtOutput.ClientID %>').val() + msg.d);
},
failure: function (msg) {
alert('Failure in CreateTriggers - ' + msg.d);
$('#txtBlocksDone').val(parseInt($('#txtBlocksDone').val()) + 1);
}
})
}

It ends up calling the following server side code contained in ajax.aspx.vb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<System.Web.Services.WebMethod()>
<Script.Services.ScriptMethod(ResponseFormat:=Script.Services.ResponseFormat.Json)>
Public Shared Function GenerateHistoryInitialize(
ByVal dbCode As String,
ByVal manageDdTable As Boolean,
ByVal tableName As String,
ByVal outputDatabaseType As String
) As String

Dim sz As String = ""

Console.WriteLine("***")
Dim dbId As String _
= dbCls.GetDbIdByDbCode(dbCode)

Dim aDb As dbCls _
= dbCls.GetOneByDbId(dbId)

Dim connectionString As String _
= aDb.StructureConnectionString

Dim databaseName As String _
= aDb.DatabaseName

Dim aTable As New si.aTable(tableName)
Dim al As System.Collections.ArrayList _
= aTable.getColumns(aDb.SampleDataDriverTypeEnum, connectionString, "", 0, "", "", "")
Dim pk As String _
= aTable.getPrimaryKeyColumnName(connectionString, tableName, aDb.SampleDataDriverTypeEnum)

Return si.CodeGenHistory.GenerateHistoryInitialize(
manageDdTable, tableName, pk,
si.Structures.ComputeDatabaseType(outputDatabaseType))

End Function