RequiredFieldValidator 在我的 ASp 站点中不起作用

本文关键字:不起作用 站点 ASp 我的 RequiredFieldValidator | 更新日期: 2023-09-27 17:56:08

我正在尝试学习一些C#和asp,同时在Visual Studio中创建预订页面。我在使用RequiredFieldValidator时遇到了一个小问题,因为当我单击按钮时,没有任何反应。

我正在使用引导 css 文件来使它更加闪亮。注册页面的一些代码在这里:

更新的代码

<asp:Content ID="RegistrationContent" ContentPlaceHolderID="MainContent" runat="server">
<br />
<div class="container">
    <h2>Registration</h2>
    <p>Please fill out the forms to complete your registration.</p>
    <form role="form">
        <div class="form-group">
            <label for="username">Name:</label>
            <asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
                CssClass="text-danger" ErrorMessage="The user name field is required." ID="RequiredFieldValidator1" ValidationGroup="Registration" />
        </div>
        <div class="form-group">
            <label for="email">Email:</label>
            <asp:TextBox runat="server" ID="Email" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
                CssClass="text-danger" ErrorMessage="The email field is required." ID="RequiredFieldValidator2" ValidationGroup="Registration" />
        </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <asp:TextBox runat="server" ID="Password" CssClass="form-control" TextMode="Password" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                CssClass="text-danger" ErrorMessage="An password is required." ID="RequiredFieldValidator3" ValidationGroup="Registration" />
        </div>
        <div class="form-group">
            <label for="confirmpassword">Confirm Password:</label>
            <asp:TextBox runat="server" ID="ConfirmPassword" CssClass="form-control" ValidationGroup="Registration" TextMode="Password" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
                CssClass="text-danger" ErrorMessage="An password is required." ID="RequiredFieldValidator4" ValidationGroup="Registration" />
        </div>
        <div class="form-group">
            <button type="button" ValidationGroup="Registration" class="btn btn-primary btn-sm">Create</button>
            &nbsp;
            <button type="reset" ValidationGroup="Registration" class="btn btn-primary btn-sm">Reset</button>
        </div>
    </form>
</div>

已经进行了验证组,但它仍然不起作用..还有其他建议吗?

RequiredFieldValidator 在我的 ASp 站点中不起作用

你只是错过了一件小事,即为您的按钮和 RequiredFieldValidators 分配一个验证组。您的代码应该是:

<div class="container">
    <h2>Registration</h2>
    <p>Please fill out the forms to complete your registration.</p>
    <form role="form">
        <div class="form-group">
            <label for="username">Name:</label>
            <asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName"
                CssClass="text-danger" ErrorMessage="The user name field is required." ValidationGroup="Group1" />
        </div>
        <div class="form-group">
            <label for="email">Email:</label>
            <asp:TextBox runat="server" ID="Email" CssClass="form-control" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Email"
                CssClass="text-danger" ErrorMessage="The email field is required." ValidationGroup="Group1" />
        </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <asp:TextBox runat="server" ID="Password" CssClass="form-control" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="Password"
                CssClass="text-danger" ErrorMessage="An password is required." ValidationGroup="Group1" />
        </div>
        <div class="form-group">
            <label for="confirmpassword">Confirm Password:</label>
             <asp:TextBox runat="server" ID="ConfirmPassword" CssClass="form-control" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="ConfirmPassword"
                CssClass="text-danger" ErrorMessage="An password is required." ValidationGroup="Group1" />
        </div>
        <div class="form-group">
            <asp:button ValidationGroup="Group1" type="button" class="btn btn-primary btn-sm">Create</asp:button>
            &nbsp;
            <asp:button ValidationGroup="Group1" type="reset" class="btn btn-primary btn-sm">Reset</asp:button>
        </div>
    </form>