1

How might I add a JavaScript message box with a confirmation option in ASP.NET?

1

4 Answers 4

2

Try using confirm :

<script>
    var userWantsToContinue = confirm("do you want to continue ?");
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

The below uses client side messagebox and redirects to a page test.aspx only if textbox contain some value.

In default.aspx:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
<script>
function validate_this()

{

if (document.getElementById("TextBox1").value.length == 0)

{
return false;
}

else
{
return confirm('Do you wish to continue');
}
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick = "javascript:return validate_this();"/>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>

In default.aspx.vb:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" Then
            Response.Redirect("test.aspx")
        End If
    End Sub
End Class

Comments

0

Use Javascript's confirm() method.

if(confirm('Are you sure?')) {
 // executed if the user clicks OK
} 

Comments

0

If you are needing to add a confirm dialog box when the user clicks a button (such as a Delete button), where clicking Cancel has the effect of canceling a postback, you can use the Button control's OnClientClick property like so:

OnClientClick="return confirm('This will permanently delete this item. Are you sure you want to do this?');"

For more information on working with client-side script in an ASP.NET WebForms application, check out: Client-Side Enhancements in ASP.NET.

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.