未显示验证摘要 (C# ASP.NET 4.5)

时间:2021-02-23 19:14:23

标签: c# asp.net validation webforms

我有一个需要有效用户输入的 ASP.NET Web 表单。但是,在输入无效用户输入时,不会显示验证摘要。奇怪的是,它工作了一段时间,然后现在根本不显示。这是我的代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>

<!DOCTYPE html>
<html>
<head>
    <title>Coding Club Registration</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
    <h1>Coding Club Registration Form</h1>
    <form runat="server" defaultbutton="Submit">
        <div>
            <asp:Label ID="NameLabel" runat="server" Text="Your Name:" AssociatedControlID="NameTextBox" AccessKey="N"></asp:Label>
            <asp:TextBox  ID="NameTextBox" runat="server" Columns="30" MaxLength="30"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="NameTextBox" CssClass="error" ErrorMessage="Name Required"><span>*</span></asp:RequiredFieldValidator>
            <br /><br />

            <asp:Label ID="EmailLabel" runat="server" Text="Your Email:" AssociatedControlID="EmailTextBox" AccessKey="N"></asp:Label>
            <asp:TextBox  ID="EmailTextBox" runat="server" Columns="30" MaxLength="30"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="EmailTextBox" CssClass="error" Display="Dynamic" ErrorMessage="Email Required"><span>*</span></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="EmailTextBox" CssClass="error" Display="Dynamic" ErrorMessage="Invalid Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
            <br /><br />

            <asp:Label ID="PhoneLabel" runat="server" Text="Your Phone:" AssociatedControlID="PhoneTextBox" AccessKey="N"></asp:Label>
            <asp:TextBox  ID="PhoneTextBox" runat="server" Columns="30" MaxLength="30"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="PhoneTextBox" CssClass="error" Display="Dynamic" ErrorMessage="Phone Required"><span>*</span></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="PhoneTextBox" CssClass="error" Display="Dynamic" ErrorMessage="Invalid Phone" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">*</asp:RegularExpressionValidator>
            <br /><br />

            <asp:Button ID="Submit" runat="server" Text="Submit Form" AccessKey="S" onclick="SubmitButton_Click" />
            <asp:Button ID="Clear" runat="server" Text="Clear Form" AccessKey="C" onclick="ClearButton_Click" CausesValidation="False" />
            <br /><br />

            <div class="container">
                <asp:Label ID="Result" runat="server" Text="" Visible="false"></asp:Label>
            </div>
            
        </div>
        <asp:ValidationSummary ID="Errors" runat="server" ForeColor="Red" />
    </form>
</body>
</html>
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            // Add form results to result label
            string newText = "<h3>The Information Entered</h3>";
            newText += "<p>Name: " + NameTextBox.Text + "</p>";
            newText += "<p>Email: " + EmailTextBox.Text + "</p>";
            newText += "<p>Phone: " + PhoneTextBox.Text + "</p>";

            Result.Text = newText;
            // Set result lable visibility to true
            Result.Visible = true;
        }
    }

    protected void ClearButton_Click(object sender, EventArgs e)
    {
        // Clear form fields
        NameTextBox.Text = "";
        EmailTextBox.Text = "";
        PhoneTextBox.Text = "";
        // Remove result visibility
        Result.Visible = false;
 //       Errors.Visible = false;
    }
}

enter image description here 任何帮助将不胜感激!

0 个答案:

没有答案
相关问题