开发者

How can I add a client-side (JavaScript) confirmation message box using ASP.NET?

How might I add a JavaScript message box with a confir开发者_如何学Pythonmation option in ASP.NET?


Try using confirm :

<script>
    var userWantsToContinue = confirm("do you want to continue ?");
</script>


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


Use Javascript's confirm() method.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜