我有一个gridview,在插入和编辑行时会进行验证。 插入工作正常。但是,在编辑行时,将触发RegularExpressionValidator以显示' Name'我点击“更新”时的字段。验证是它必须少于255个字符。该字段小于该字段,但始终显示错误消息。我使用此验证在另一个网格中编辑,它工作正常。我无法弄清楚错误被触发的原因。
这是网格的标记:
<asp:GridView ID="SectionGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Section Information" CaptionAlign="Top"
CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true"
OnPageIndexChanging="SectionGridView_PageIndexChanging" OnRowDataBound="SectionGridView_RowDataBound" OnRowCommand="SectionGridView_RowCommand"
OnRowCancelingEdit="SectionGridView_RowCancelingEdit" OnRowEditing="SectionGridView_RowEditing" OnRowUpdating="SectionGridView_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="SectionID" SortExpression="cgvSectionID">
<ItemTemplate>
<asp:Label ID="sgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CourseID">
<EditItemTemplate>
<asp:DropDownList ID="sgvDdlCourseID" runat="server" ></asp:DropDownList>
<asp:Label ID="sgvEditLblCourseID" runat="server" Visible="false" Text='<%# Bind("CourseID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblCourseID" runat="server" Text='<%# Bind("CourseID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertCourseID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="sgvName">
<EditItemTemplate>
<asp:TextBox ID="sgvEditName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditName" ControlToValidate="sgvEditName" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditName" ControlToValidate="sgvEditName" runat="server"
ErrorMessage="Maximum length is 255." ValidationGroup="EditSectionValidation" ValidationExpression="^{1,255}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertName" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertName" ControlToValidate="sgvInsertName" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertName" ControlToValidate="sgvInsertName" runat="server"
ErrorMessage="Maximum length is 255." ValidationGroup="InsertSectionValidation" ValidationExpression="^{1,255}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="SKU" SortExpression="sgvSKU">
<EditItemTemplate>
<asp:TextBox ID="sgvEditSKU" runat="server" Text='<%# Bind("SKU") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditSKU" ControlToValidate="sgvEditSKU" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditSKU" ControlToValidate="sgvEditSKU" runat="server"
ErrorMessage="Enter alphanumeric, hyphen, or forward slash. Maximum length is 100." ValidationGroup="EditSectionValidation" ValidationExpression="^[\w-/]{1,100}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblSKU" runat="server" Text='<%# Bind("SKU") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertSKU" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertSKU" ControlToValidate="sgvInsertSKU" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertSKU" ControlToValidate="sgvInsertSKU" runat="server"
ErrorMessage="Enter alphanumeric, hyphen, or forward slash. Maximum length is 100." ValidationGroup="InsertSectionValidation" ValidationExpression="^[\w-/]{1,100}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Registration Open Date" SortExpression="sgvRegOpenDate">
<EditItemTemplate>
<asp:TextBox ID="sgvEditRegOpenDate" runat="server" CssClass="datepick" Text='<%# Bind("RegistrationOpen", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblRegOpenDate" runat="server" Text='<%# Bind("RegistrationOpen", "{0:MM-dd-yyyy}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertRegOpenDate" runat="server" CssClass="datepick" Width="90%" ></asp:TextBox>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Registration Close Date" SortExpression="sgvRegCloseDate">
<EditItemTemplate>
<asp:TextBox ID="sgvEditRegCloseDate" runat="server" CssClass="datepick" Text='<%# Bind("RegistrationClose", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblRegCloseDate" runat="server" Text='<%# Bind("RegistrationClose", "{0:MM-dd-yyyy}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertRegCloseDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Section Start Date" SortExpression="sgvSecStartDate">
<EditItemTemplate>
<asp:TextBox ID="sgvEditSecStartDate" runat="server" CssClass="datepick" Text='<%# Bind("SectionStart", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblSecStartDate" runat="server" Text='<%# Bind("SectionStart", "{0:MM-dd-yyyy}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertSecStartDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Section End Date" SortExpression="sgvSecEndDate">
<EditItemTemplate>
<asp:TextBox ID="sgvEditSecEndDate" runat="server" CssClass="datepick" Text='<%# Bind("SectionEnd", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblSecEndDate" runat="server" Text='<%# Bind("SectionEnd", "{0:MM-dd-yyyy}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertSecEndDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Price">
<EditItemTemplate>
<asp:TextBox ID="sgvEditMemberPrice" runat="server" Text='<%# Bind("MemberPrice", "{0:f}") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditMemberPrice" ControlToValidate="sgvEditMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditMemberPrice" ControlToValidate="sgvEditMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="EditSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblMemberPrice" runat="server" Text='<%# Bind("MemberPrice", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertMemberPrice" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertMemberPrice" ControlToValidate="sgvInsertMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertMemberPrice" ControlToValidate="sgvInsertMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="InsertSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Non-member Price">
<EditItemTemplate>
<asp:TextBox ID="sgvEditNonMemberPrice" runat="server" Text='<%# Bind("NonMemberPrice", "{0:f}") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditNonMemberPrice" ControlToValidate="sgvEditNonMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditNonMemberPrice" ControlToValidate="sgvEditNonMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="EditSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="sgvLblNonMemberPrice" runat="server" Text='<%# Bind("NonMemberPrice", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sgvInsertNonMemberPrice" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertNonMemberPrice" ControlToValidate="sgvInsertNonMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertNonMemberPrice" ControlToValidate="sgvInsertNonMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="InsertSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
<HeaderStyle ForeColor="Black" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="sgvImgUpdate" runat="server" CausesValidation="True" ValidationGroup="EditSectionValidation" CommandName="Update" Text="Update"></asp:Button>
<asp:Button ID="sgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="sgvEditButton" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" Width="40%"></asp:Button>
<asp:Button ID="sgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" Width="40%"></asp:Button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:Button ID="sgvBtnAdd" runat="server" CommandName="Add" Text="Add Section" Width="90%" CausesValidation="true" ValidationGroup="InsertSectionValidation" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr>
<td colspan="11" style="text-align:center;">
No section data was found for this course. Sections can be added to this course by clicking the 'Add Section' Button.
</td>
</tr>
<tr>
<td></td>
<td><asp:TextBox ID="sgvInsertCourseID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="sgvInsertName" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertNameEmpty" ControlToValidate="sgvInsertName" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertNameEmpty" ControlToValidate="sgvInsertName" runat="server"
ErrorMessage="Maximum length is 255." ValidationGroup="InsertSectionValidation" ValidationExpression="^{1,255}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:TextBox ID="sgvInsertSKU" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertSKUEmpty" ControlToValidate="sgvInsertSKU" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertSKUEmpty" ControlToValidate="sgvInsertSKU" runat="server"
ErrorMessage="Enter alphanumeric, hyphen, or forward slash. Maximum length is 100." ValidationGroup="InsertSectionValidation" ValidationExpression="^[\w-/]{1,100}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:TextBox ID="sgvInsertRegOpenDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="sgvInsertRegCloseDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="sgvInsertSecStartDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="sgvInsertSecEndDate" runat="server" CssClass="datepick" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="sgvInsertMemberPrice" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertMemberPriceEmpty" ControlToValidate="sgvInsertMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertMemberPriceEmpty" ControlToValidate="sgvInsertMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="InsertSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:TextBox ID="sgvInsertNonMemberPrice" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValInsertNonMemberPriceEmpty" ControlToValidate="sgvInsertNonMemberPrice" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValInsertNonMemberPriceEmpty" ControlToValidate="sgvInsertNonMemberPrice" runat="server"
ErrorMessage="Enter currency format." ValidationGroup="InsertSectionValidation" ValidationExpression="^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:Button ID="sgvBtnAddEmpty" runat="server" CommandName="Add" Text="Add Section" Width="90%" CausesValidation="true" ValidationGroup="InsertSectionValidation" /></td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
虽然在触发验证之前没有使用此方法,但这是后面的代码。万一你需要它......
protected void SectionGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)SectionGridView.Rows[e.RowIndex];
//Save the current section and course selections
int tiSectionID = Convert.ToInt32(ddlSections.SelectedValue.ToString());
int tiCourseID = Convert.ToInt32((row.FindControl("sgvDdlCourseID") as DropDownList).SelectedValue);
string tstrName = (row.FindControl("sgvEditName") as TextBox).Text;
string tstrSKU = (row.FindControl("sgvEditSKU") as TextBox).Text;
Nullable<DateTime> tdtRegOpenDate = setStartTime((row.FindControl("sgvEditRegOpenDate") as TextBox).Text);
Nullable<DateTime> tdtRegCloseDate = setEndTime((row.FindControl("sgvEditRegCloseDate") as TextBox).Text);
Nullable<DateTime> tdtSecStartDate = setStartTime((row.FindControl("sgvEditSecStartDate") as TextBox).Text);
Nullable<DateTime> tdtSecEndDate = setEndTime((row.FindControl("sgvEditSecEndDate") as TextBox).Text);
decimal tdMemberPrice = Convert.ToDecimal((row.FindControl("sgvEditMemberPrice") as TextBox).Text);
decimal tdNonMemberPrice = Convert.ToDecimal((row.FindControl("sgvEditNonMemberPrice") as TextBox).Text);
DAL.updateSections(tiSectionID, tiCourseID, tstrName, tstrSKU, tdtRegOpenDate, tdtRegCloseDate, tdtSecStartDate, tdtSecEndDate, tdMemberPrice, tdNonMemberPrice);
//Refresh the section grid to include the updated row
SectionGridView.EditIndex = -1;
populateSectionGrid();
//Refresh the dropdown list for sections
populateDropDownSectionList(tiSectionID);
}
答案 0 :(得分:1)
你的正则表达式很糟糕。
ValidationExpression="^{1,255}$"
^是字符串的开头,$是字符串的结尾,{x.y}适用于前一个元素。所以这里它会寻找1-255个起始字符串,你可以考虑使用外卡。所以它会匹配任何角色,只要它们有1-255个。
ValidationExpression="^.{1,255}$"
我将模板拉到空白页面,并更改了正则表达式,验证不再触发每个测试。
<asp:TextBox ID="sgvEditName" runat="server" Text=""></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditName" ControlToValidate="sgvEditName" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSectionValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditName" ControlToValidate="sgvEditName" runat="server"
ErrorMessage="Maximum length is 255." ValidationGroup="EditSectionValidation" ValidationExpression="^.{1,255}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
<asp:Button runat="server" ID="btnTest" ValidationGroup="EditSectionValidation" />