asp.net表单获取自动提交

时间:2014-11-26 01:39:46

标签: javascript c# asp.net

我在提交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()方法的”其他条件的警告提示,我的表单总是会被发布。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

试试这个你没有返回功能 所以你必须返回功能......如下所示......

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