0
votes

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()

4
the shortest way to write it is return confirm('Are you serious?'); But you say the page gets posted to the server and the event executes...so why do you think that is so?deostroll
I know about this way but problem is that I want to get text entered in txtSubject. And about posting to server I debugged it.Ami
so what is the problem on that front you are facing?deostroll
I dont want to call click event if user clicks on CancelAmi
That is expected behavior. In other words, and greatly simplified, OnClientClick="javascript:return false;" should not trigger a postback. You need to debug and find out why confirm(...) is actually returning true in your case...deostroll

4 Answers

1
votes
  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;
         }
  }
1
votes

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;
     }
 }
0
votes

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;
 }
0
votes

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;                
        }