I am trying this with Asp .net web application
I am trying to write some javascript for a confirmation dialog before inserting data into database.
Script is as below
To call showconfirm() function I use OnClientClick ="showconfirm()"
My button look like as follows.
asp:Button ID="Insert" runat="server" Text="Insert" OnClientClick ="showconfirm()" />
I am using code behind technique. Now here if I don't use OnClick="Insert_Click" then I will not able to insert the data.
And if I use OnClick="Insert_Click" I will be inserting data every time wether I am clicking OK or cancle in confirmation dialog.
I am not able to understand where i should use OnClick="Insert_Click" event so that on OK click I should be able to insert data and on cancle it should not insert data.
So this problem comes when button tag is like this
Please suggest some thing.
Niradhip ChakrabortyPosted Jun 5, 2009, 12:26 PM
Your html code is as follows.
<asp:Button ID="btnCancel" CssClass="Btn" runat="server"
Text="Cancel" OnClick="btnCancel_Click" />
Your Javascript code:
function AskConfirm()
{
var answer;
answer = confirm("Are you sure you want to submit this Booking?");
if (answer == false)
{
return false;
}
return true;
}
Your pagebehind code:
protected void Page_Load(object sender, EventArgs e)
{
btnCancel.Attributes.Add("onclick", "javascript:return AskConfirm();");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("OnlineReservation.aspx");
}
Instead of calling the javascript function in html button code I have called it from pagebehind. You can use any one of the method.
Now as soon as you hit the cancel button it will ask for confirmation if you say no it will go to javascript code first and will found the if loop condition where we check answer == false and inside the loop as we mentioned return false,it will stop execution and it will not execute any further code. But in case of answer = true it will not go inside the loop and it will find return true at the end of function,which allows to go to server side assosiated with the cancel button in server side and execute it.
Niradhip ChakrabortyPosted Jun 5, 2009, 4:15 PM
Deepak GoyalPosted Jun 5, 2009, 2:13 PM