I have very simple web page with one text box, two buttons. In text button I write username and by clicking on search button I get all users that match the search parameter and in code behind create table radio buttons and store the results in this table After that I need to choose radio button and by clicking on second button to send the values of row selected to server to make some manipulation and to response by true or false. On clicking on second button I use ajax to send selected value to server: The button:
<asp:Button ID="Button2" OnClientClick="retrieveTableValues(event)" onclick="Button2_Click"
CssClass="btn btn-primary" runat="server" Height="31px" Width="92px" Text="Move User"/>
Ajax:
$.ajax({
type: "POST",
url: "MoveUsersWebForm.aspx/MoveUser",
data: JSON.stringify({ 'userToMove': rowValues, 'ouToMove': selectedDropDownListItem, 'userCurrentOU': userCurrentOU}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if ( if (response.d)) {
alert("The user was succefully moved");
}
else {
alert("Some problem occurred");
}
},
});
After some manipulations on server I need to response to ajax with true of false and based on it to show alert on web page. The problem is that I get alert, only when the response is false, but when it's true nothing happens. Is there something I miss here?
I try to send a string. Tried to get response.succees.
What data type is the web method setup as?
perhaps the web method is returning a string?
this should work:
if ( response.d) {
alert("The user was successfully moved");
}
else {
alert("Some problem occurred");
}
Or, if a string, then is that "True", or "true"????
So, then this:
if ( response.d = "true") {
alert("The user was successfully moved");
}
else {
alert("Some problem occurred");
}
So, what data type is the web method?
but, don't use a if() like you have, just use if (response.d) if the web method is Boolean. If it is a string, then careful with case.
I have seen in some cases, a -1, or other return types be cased wrong, so in some cases, return a string of "true" or "false", and use a string compare in the js like (response.d == "true")
You have this for your button:
<asp:Button ID="Button2"
OnClientClick="retrieveTableValues(event)"
onclick="Button2_Click"
CssClass="btn btn-primary"
runat="server"
Height="31px"
Width="92px" Text="Move User"/>
Ok, so with above you have BOTH a server side click, and that of a client side click.
but, KEEP in mind that means BOTH the client side code will run, and your server side. With a page post-back, then your client side code is MUCH going to get blown out of the water with that page post-back. (and then you get a WHOLE new browser page sent back, and your local js code not going to really get a chance to run).
Remember, ajax does NOT halt, nor wait. So, when you click that button the client side code RUN RIGHT though ALL of that js code. The routine DOES NOT wait, and thus/then your server side button event runs.
Since that ajax code does NOT halt, nor wait for the server ajax call, then your server side button code also runs right away.
You thus can't have that server side code run, or at the very least don't want it to run until the ajax call is done.
Or do you need/want the server side code of that button to run first?
It is not clear which case you need/want.
However, you CAN make the server side code wait to run, the simple trick is to have your ajax routine return "false".
eg this:
<asp:Button ID="Button2"
OnClientClick="return retrieveTableValues(this)"
onclick="Button2_Click"
CssClass="btn btn-primary"
runat="server"
Height="31px"
Width="92px" Text="Move User"/>
Note in above, how we PASS the button!!!
So, now our js code is this:
var oktoclick = false
function retrieveTableValues(btn)
if (oktoclick) {
oktoclick = false
return true
}
$.ajax({
type: "POST",
url: "MoveUsersWebForm.aspx/MoveUser",
data: JSON.stringify({ 'userToMove': rowValues, 'ouToMove': selectedDropDownListItem, 'userCurrentOU': userCurrentOU}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if ( if (response.d)) {
alert("The user was succefully moved");
oktoclick = true;
$(btn).click()
}
else {
alert("Some problem occurred");
}
},
});
return false;
So, note what we done in above.
You click your button, it does the ajax call, CODE RUNS right though, returns false. (so server side onclick event does NOT run).
We wait for ajax call to complete, it returns, runs "success".
We then set our flag = true, and "click" the same button again!!!
Now, this time, the client side code runs again, but returns true, and exits. That means the server side click event for that button now runs.
So, just keep in mind that ajax and in fact MOST js code does not halt, nor wait, but runs asynchronously. So, when you call that js function with the ajax call, the code FALLS + runs right though to the end, and does NOT halt, nor wait for the return "success".
So, note how we setup the client side click event of the button to return true, or false. Until such time a "true" is returned, then the server side event code attached to the button click will not run.
This also by the way is how we can attached a "confirm" dialog to a button click - even ones like sweetalert, or say jQuery.UI dialogs, which like most js code runs as noted asynchronously.
Blocking code in js is NOT allowed. the only exceptions are alert(), and confirm(), and they are heading for depreciation rather soon.