Reputation: 397
I am having a asp.net button control and a javascript confirmation box which is executed on that button click. If the javascript returns true than only perform click event of button..
HTML:
<asp:TextBox ID="txtSubject" runat="server" CssClass="textbox" Width="454px" CausesValidation="true" onfocus="ddlSelect()"></asp:TextBox>
<asp:Button ID="btnSaveSend" CssClass="myButton" runat="server" Text="Save & Send"
OnClientClick="javascript:return SubjectEmpty()" OnClick="btnSaveSend_Click" />
Javascript:
function SubjectEmpty() {
var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
var result = confirm("Are you sure you want to send mail:" + subject + " ? We shell check for the availability..");
if (result == true) {
return true;
}
else {
return false;
}
Code Behind:
protected void btnSaveSend_Click(object sender, EventArgs e)
{
//Do something
}
The problem is even if I select cancel it executes btnSaveSend_click()
Upvotes: 0
Views: 1349
Reputation: 397
Sorry guys my mistake..
I had used ajax control in my page. So it was performing click event even if I click on cancel. I didnt knew it earlier that this was due to ajax. So I got other way out:
if (!confirm("Are you sure you want to send mail:" + subject + " of type "+type+" on " + sendDate + " at " + hour + ":" + min + ":00.. We shell check for the availability..")) {
document.getElementById('<%=btnSaveSend.ClientID%>').disabled = true;
}
Upvotes: 0
Reputation: 1838
Try this..You should add a return false;
to block the server event from firing..
function SubjectEmpty() {
var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
var result = confirm("Are you sure you want to send mail:" + subject + " ? We shall check for the availability..");
if (result == true) {
return true;
}
else
{
return false;
}
}
Upvotes: 1
Reputation: 2542
function SubjectEmpty() {
var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
if (confirm("Are you sure you want to send mail:" + subject + " ? We shell
check for the availability.."))
{
return true;
}
else
{
return false;
}
}
Upvotes: 1
Reputation: 9126
Change your Java script function like below. It will help you.
function SubjectEmpty() {
var subject = document.getElementById("<%=txtSubject.ClientID%>").value;
var result = confirm("Are you sure you want to send mail:" + subject + " ? We shell check for the availability..");
if (result == true) {
return true;
}
return false;
}
Upvotes: 0