I am calling a javascript function to return yes,no values from the user in a button click event inside an update panel. I want to call certain server side functions depending on user action (yes/no). My javascript code is given below
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
I am calling this in my server side button click which is shown below.
if ((Convert.ToInt32(_dsLeaveDetails.Tables[0].Rows[0][0]) == 1)
{
ShowAlert("Leave is already marked for this date");
return;
}
else if ((Convert.ToInt32(_dsAttendanceDetails.Tables[0].Rows[0][0]) >= 1))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm();", true);
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('You clicked YES!');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('You clicked YES!');", true);
}
return;
}
The issue is, pop up is showing only after the code is fully executed and because of that user action (yes/no) cannot be processed further.
Also I cannot call javascript function on clientclick event because immediately I dont require a pop up. Only after checking the dataset dsAttendanceDetails, I need the pop up. Kindly help.
You seems to assume that the line
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm();", true);would execute the java-script function immediately – but that’s simply not possible. This call would execute the JS function when response reaches to the client i.e. when subsequent server code would have been already executed.The java-script function would be executed at the client side and you will need a form submit (sync or asynchronous does not matter) to retrieve the user’s choice at the server side. So you need to two requests – first would register the script that would confirm user choice and subsequent request to send the user’s choice to the server.