I have an asp web application and need to call a JavaScript confirm function from within a C# if statement. I am a very very weak on JavaScript so be gentle. Here is my JavaScript I have placed on the ASPX page:
function Confirm()
{
var result = window.confirm('Are you sure?');
if (result == true)
return true;
else
return false;
}
here is the C# code when I want to call this script. What I am trying to accomplish is check 2 values on my aspx page the first is a CaseID and the second in the value of a checkbox. If the code runs I want to show the confirmation box and if they click yes then redirect to another page.
else if (rec.Count != 0 && rec.Select(s=> s.IsClosed).First().Value == true && chkOverRide.Checked == false)
{
//if the case is closed and not overridded redirects to edit page
ScriptManager.RegisterStartupScript(
this,
GetType(),
"key",
"confirm('Are you sure you want to continue?');",
true);
Response.Redirect("~\\Forms\\EditRecord.aspx?CaseId="+ rec.Select(s => s.CaseNum).First(), false);
return false;
}
I have tried this but nothing happens I do not get a confirmation box the code just falls thru and runs the redirect. Thanks
As I worked thru this problem I found I had some issues that were causing my failures. The first issue was I was using a C# redirect which was causing a post back so my JavaScript code never showed the JavaScript box. I had to rethink the process and ended up using JavaScript to do the redirect. Here is what the new JavaScript looks like.
function Confirm(caseId) {
var result = window.confirm('This case already exists and completed. Please reopen it for further updates?');
if (result == true)
window.location = '\EditRecord.aspx?CaseNum='+caseId;
else
return false;
}
And the C# Method now looks like this:
else if (rec.Count != 0 && rec.Select(s=> s.IsClosed).First().Value == true && chkOverRide.Checked == false)
{//if the case is closed and not overridded redirects to edit page
ScriptManager.RegisterStartupScript(this,GetType(),"key", "Confirm('" + CaseNumber + "');", true);
Method now works as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.