RequiredFieldValidator在我的ASp网站

时间:2015-06-15 08:25:15

标签: c# css asp.net twitter-bootstrap

我试图学习一些C#& amp; asp,在Visual Studio中创建预订页面。我遇到了RequiredFieldValidator的一个小问题,因为当我点击按钮时,没有任何反应。

我使用bootstrap 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>

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

1 个答案:

答案 0 :(得分:0)

您只是错过了一件小事,即将验证组分配给您的按钮和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>