表单视图中验证控件的问题.净c#

本文关键字:问题 控件 视图 验证 表单 | 更新日期: 2023-09-27 17:54:41

我在formview中的验证控制有一些问题。我可以在formview控件中添加一个验证控件,它工作得很好,直到我试图使用validationgroups对它们进行分组。此外,如果我不输入一个值的程序仍然允许在数据库中添加它。有人能帮我指出程序中的问题或错误吗?这是我写的Insert语句。

<InsertItemTemplate>
                Book ID:
                <asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
                <asp:RequiredFieldValidator ID="RequesFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="bookidTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" Display="Dynamic" ControlToValidate="bookidTextBox" ValidationExpression="^(['S's]{13,13})$" ErrorMessage="Value must be at least 13 characters long">
                </asp:RegularExpressionValidator>
                <br />
                Title:
                <asp:TextBox ID="booktitleTextBox" runat="server" 
                    Text='<%# Bind("booktitle") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="booktitleTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                Author's lastname:
                <asp:TextBox ID="lastnameTextBox" runat="server" 
                    Text='<%# Bind("lastname") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="lastnameTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                Author's firstname:
                <asp:TextBox ID="firstnameTextBox" runat="server" 
                    Text='<%# Bind("firstname") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="* Required" ControlToValidate="firstnameTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                Description:
                <asp:TextBox ID="descriptionTextBox" runat="server" 
                    Text='<%# Bind("description") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required" ControlToValidate="descriptionTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                Category:
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="categoryDataSource" DataTextField="name" 
                    DataValueField="categoryid" SelectedValue='<%# Bind("categoryid", "{0}") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="categoryDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
                    SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
                </asp:SqlDataSource>
                <br />
                Date added:
                <asp:TextBox ID="dateaddedTextBox" runat="server" 
                    Text='<%# Bind("dateadded") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="* Required" ControlToValidate="dateaddedTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                Status:
                <asp:DropDownList ID="DropDownList2" runat="server" 
                    DataSourceID="statusDataSource" DataTextField="statusname" 
                    DataValueField="statusid" SelectedValue='<%# Bind("statusid", "{0}") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="statusDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
                    SelectCommand="SELECT [statusid], [statusname] FROM [BookStatus]">
                </asp:SqlDataSource>
                <br />
                Quantity:
                <asp:TextBox ID="quantityTextBox" runat="server" 
                    Text='<%# Bind("quantity") %>' />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="* Required" ControlToValidate="quantityTextBox" ValidationGroup="InsertBook">
                </asp:RequiredFieldValidator>
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Add" />
                    &nbsp;
                <asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>

任何帮助都将非常感激;)

表单视图中验证控件的问题.净c#

您必须设置ValidationGroup="InsertBook",因为您已将验证组设置为验证。

 <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Add" ValidationGroup="InsertBook" />