Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working in asp.net development, I have Two files i.e.
i) UserControl
ii).js (javascript)
In .js File following is code
var varBindNewSalesTranGrid = '';
function BindNewSalesAttribute() {
    $.ajax({
        type: "POST",
        url: "/url of Webmthod returns string",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            varBindNewSalesTranGrid += data.d;
        },
        error: function (xhr, ajaxOptions, thrownError) {
        }
    });
}

In Usercontrol file
 $(document).ready(function () {
        BindNewSalesAttribute();
        alert(varBindNewSalesTranGrid);
    });
//Which is file referenced above .js file

When I run my usercontrol file I see the value of varBindNewSalesTranGrid is empty ('').Function returns value 'TestString' and I am assigning value varBindNewSalesTranGrid in BindNewSalesAttribute function but still value is not getting updated.

if you have any ideas then let me know.
Posted
Updated 13-Oct-14 5:17am
v2
Comments
[no name] 13-Oct-14 11:59am    
The variable should be declared inside the Document.ready so that it gets memory on the page load itself, once the script is loaded.

1 solution

In the above code you are assigning the value to the global variable inside a ajax call success function. If you look into the steps of execution in $(doument).ready function it first calls the "BindNewSalesAttribute()" function which is asynchronous in nature. Then it executes the "alert(varBindNewSalesTranGrid)" statement which do not wait for completion of "BindNewSalesAttribute()" method due to asynchronous nature of the function.

Thats why when alert is executed the value has not been updated.
But in background the value is being updated and you can check it by placing the alert inside the success function or by making "async:fasle" in ajax call.

Hope this helps:)
 
Share this answer
 
Comments
RAHUL(10217975) 13-Oct-14 14:57pm    
Yes value is being updated in success function but I am not able to receive updated value in my usercontrol..How should I achieve that? Its still showing empty in usercontrol
Satya Ranjan Sahoo 14-Oct-14 1:47am    
Make "async:fasle" in ajax call:
function BindNewSalesAttribute() {
$.ajax({
type: "POST",
url: "/url of Webmthod returns string",
async: false,
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
varBindNewSalesTranGrid += data.d;
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
}
This will work but its not good to have "async:fasle" in an ajax call.Its better that perform your task related to that global variable inside the success function of ajax call.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900