簡體   English   中英

如何使用ASP.NET添加客戶端(JavaScript)確認消息框?

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

如何在ASP.NET中添加帶有確認選項的JavaScript消息框?

嘗試使用確認:

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

下面使用客戶端消息框,僅在文本框包含某些值時才重定向到頁面test.aspx

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>

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

使用Javascript的Confirm()方法。

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

如果您需要在用戶單擊按鈕(例如“刪除”按鈕)時添加確認對話框(單擊“取消”具有取消回發的效果),則可以使用Button控件的OnClientClick屬性,如下所示:

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

有關在ASP.NET WebForms應用程序中使用客戶端腳本的更多信息,請參見:ASP.NET中的客戶端增強

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM