1

I have a button with OnClick event on my web page.

<asp:TextBox ID="tbMailID" runat="server" Width="250px"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />

C# code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    bool IsGmail = CheckMailId(tbMailID.Text);
    if(!IsGmail)
    {
        string strScript = "confirm('Mail id is not from gmail, do you still want to continue?');";
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", strScript, true);
    }
    SendMail(); 
}

The page accepts a mail id in textbox and on submit sends a mail. But if mail-id is not an gmail id (validated by CheckMailId) i want to show a confirmation box to user whether he/she wants to get a mail, based on the Yes/No clicked.

But the javascript will get call after the submit event is served and mail is sent. if i add a return SendMail() will never get calls

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", strScript, true);
return;

What could be a possible solution here?

Note: the code posted is simplified version of my real use case. CheckMailId - is just a name to show, it calls up many other functions and do few db process. So incorporating that in javascript, i would like to avoid.

1 Answer 1

2

You can call a script before an event like this:

<script type="text/javascript">
    function Confirm() {
       var yourstring = document.getElementById('<%= tbMailID.ClientID %>').value;
       if (/@gmail\.com$/.test(yourstring)) {
             return true;
               }
             else
              {
        if (confirm("Mail id is not from gmail, do you still want to continue?") == true)
            return true;
        else
            return false;
    }
       }
</script>
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return Confirm();" OnClick="btnSubmit_Click" Text="Submit" />
Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't call the server-side CheckMailId()
Actually the code i have posted is simplified version of my real use case. CheckMailId - is just a name to show, it calls up many other functions and do few db process. Although your solution is perfect to use for the suedo case.

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.