0

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

5
  • 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? Commented May 12, 2014 at 11:54
  • 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. Commented May 12, 2014 at 12:08
  • so what is the problem on that front you are facing? Commented May 12, 2014 at 12:15
  • I dont want to call click event if user clicks on Cancel Commented May 12, 2014 at 12:43
  • 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... Commented May 12, 2014 at 13:03

4 Answers 4

1
  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;
         }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

@Ami its just the usage of if condition which prevents its from firing the OnClick event .
This is also not working. Don't understand what is the problem
1

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

1 Comment

It will fire the OnClick event in else part too.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.