asp.net 表单自动提交

asp.net form getting auto submitted

本文关键字:提交 表单 net asp      更新时间:2023-09-26

我试图在提交 asp.net 表单之前进行验证。

然而,它不断被提交。下面是代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #form1 {
            height: 254px;
            width: 905px;
        }
        .tabClass {
            margin-top: 10%;
        }
        input {
            margin-top: 50px;
        }
        #Name {
            margin-right: 58px;
        }
        #uName {
            margin-right: 55px;
        }
        #p1 {
            margin-right: 66px;
        }
        #Label2 {
            margin-right: 20px;
        }
    </style>
    <script type="text/javascript">
        function DoValidation(parameter) {
            document.getElementById("Button2").click();
            var prompt = "";
            var a = document.getElementById("TextBox_FName").value;
            if (a == "")
                prompt += "Please Enter Full Name. ";
            a = document.getElementById("TextBox_UName").value;
            if (a == "")
                prompt += "Please User Name. ";
            a = document.getElementById("Password1").value;
            if (a == "")
                prompt += "Please Enter Password. ";
            if (a != document.getElementById("Password2").value)
                prompt += "Entered Passwords Don't match. ";
            if (prompt === "") {
                document.getElementById("<%=Button2.ClientID%>").click();
            } else {
                alert(prompt);
                return false;
            }
        }
</script>
</head>
<body>
    <div style="margin-left: 30%;">
        <form id="form1" runat="server">      
        <div>    
            <asp:Label ID="Name" runat="server" Text="Full Name" CssClass="tabClass"></asp:Label>
            <asp:TextBox ID="TextBox_FName"  Height="25px" Width="300px" runat="server" ></asp:TextBox>    
        </div>
        <asp:Label ID="uName" runat="server" Text="User Name" CssClass="tabClass"></asp:Label>
        <asp:TextBox ID="TextBox_UName"  Height="25px" Width="200px" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="p1" runat="server" Text="Password" CssClass="tabClass"> </asp:Label>
        <asp:TextBox ID="Password1" runat="server" Height="25px"  TextMode="Password" Width="200px"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Retype Password"></asp:Label>
        <asp:TextBox ID="Password2" runat="server" Height="25px"  TextMode="Password" Width="200px"></asp:TextBox>
        <p>
           <asp:Button ID="Button3" runat="server"  OnClientClick="return !!DoValidation()"  Text="Register" style="display:none;" />
        </p>
         <asp:Button ID="Button2" runat="server"  OnClick="Button1_Click" Text="HiddenRegister" style="display:none;"/>     
        </form>
        <button onclick="DoValidation()">Reg1 </button>
    </div>
</body>
</html>

即使我收到"doValidation() 方法的其他条件"的警报提示,我的表单也总是被发布。

啊,删除DoValidation函数中的第一行:

document.getElementById("Button2").click();

试试这个,你不会重新设置函数所以你必须返回函数...像下面....

<button onclick="return DoValidation()">Reg1 </button>