RequiredFieldValidator仅适用于一次验证

时间:2015-03-20 15:37:09

标签: asp.net visual-studio-2010 validation gridview

我有一个用VS2010编写的Web应用程序。 我有一个GridView,我想在编辑模式下为一个字段添加2个不同的验证。一个验证是它是必填字段。另一个验证是最大值。长度为80个字符。 以下是我的HTML:

<asp:GridView ID="UserInfoGridView" runat="server" AutoGenerateColumns="False" 
        Caption="User Information" CaptionAlign="Top" CssClass="grid" HorizontalAlign="Left" 
        PageSize="1" Width="400px" ShowHeaderWhenEmpty="true" Height="60px"
        OnRowCancelingEdit="UserInfoGridView_RowCancelingEdit" 
        OnRowEditing="UserInfoGridView_RowEditing" 
        OnRowUpdating="UserInfoGridView_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Name" ItemStyle-Wrap="false">
                <EditItemTemplate>
                    <asp:TextBox ID="uigvTxtBoxName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>   
                     <asp:RequiredFieldValidator ID="RequiredFieldEditName" ControlToValidate="uigvTxtBoxName" runat="server"
                        ErrorMessage="Required field." ValidationGroup="EditUserNameValidation" Display="Dynamic" 
                        CssClass="message-error">
                    </asp:RequiredFieldValidator>                   
                     <asp:RequiredFieldValidator ID="MaxValEditName" ControlToValidate="uigvTxtBoxName" runat="server"
                        ErrorMessage="Maximum length is 80." ValidationGroup="EditUserNameValidation" Display="Dynamic" 
                        CssClass="message-error" ValidationExpression="^.{1,80}$">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="uigvLblName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email Address" ItemStyle-Wrap="false">
                <EditItemTemplate>
                    <asp:TextBox ID="uigvTxtBoxEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:TextBox>      
                     <asp:RequiredFieldValidator ID="RequiredFieldEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
                        ErrorMessage="Required field." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error">
                    </asp:RequiredFieldValidator>            
                     <asp:RequiredFieldValidator ID="MaxValEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="uigvLblEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Action" ShowHeader="False" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
                <EditItemTemplate>
                    <asp:Button ID="uigvUpdateButton" runat="server" CausesValidation="True" CommandName="Update" 
                                    Text="Update" CssClass="gridActionbutton"></asp:Button>
                    &nbsp;<asp:Button ID="uigvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" 
                                    Text="Cancel" CssClass="gridActionbutton"></asp:Button>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="uigvEditButton" runat="server" CausesValidation="False" CommandName="Edit" 
                                Text="Edit" CssClass="gridActionbutton">
                    </asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle Wrap="True" />
    </asp:GridView>

当处于编辑模式时,如果该字段留空,则会显示两个错误消息。如果用户输入的字符超过80个,则不会显示任何消息。 我在使用VS 2012的应用程序中这样做了。可以在VS2010中完成吗?如果是的话,我哪里错了?

感谢。

1 个答案:

答案 0 :(得分:1)

使用regularexpression验证器或范围验证器代替requiredfield验证器来验证最大长度。

因此,您需要使用一个必填字段,另一个是正则表达式验证器。