C# IsVaild not flipping to False when asp.net edit template validation control group runs and should return false
我有一个页面可以为我们的人力资源系统跟踪公司假期,但是现在如果您尝试编辑假期的结束日期并且用户不小心输入了一个早于开始日期的结束日期,这一切仍然有效为 true 并允许将编辑写入数据库表。
我错过了什么使页面始终验证为 True?我在此页面上还有一个 DetailsView,它使用一组验证控件,它正在工作,并且当某些内容未通过验证时将验证为 false。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | protected void HolidaysDS_Updated(object sender, SqlDataSourceStatusEventArgs e) { if (e.Exception != null) { lblNullUpdate.Text ="One of your updated fields is invalid. Your update did not occur."; lblNullUpdate.Visible = true; e.ExceptionHandled = true; } } protected void gvHolidays_RowUpdating(object sender, GridViewUpdateEventArgs e) { Page.Validate("EditValidationControls"); if (Page.IsValid == false) { e.Cancel = true; } lblNullUpdate.Visible = false; } |
如果用户在更新中传递 null 以及应该验证页面并检查所有内容是否有效的事件,这就是我当前的 C# 处理异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | Holidays </asp:Label> <asp:DropDownList ID="ddlHolidayYears" runat="server" AutoPostBack="True" DataSourceID="HolidayYearDS" DataTextField="HYear" DataValueField="HYear"> </asp:DropDownList> <asp:GridView ID="gv_Holidays" runat="server" AutoGenerateColumns="False" DataKeyNames="HolidayID" DataSourceID="HolidaysDS" AllowSorting="True" CssClass="gvhours" HeaderStyle-CssClass="header" AlternatingRowStyle-CssClass="alternating" SortedAscendingHeaderStyle-CssClass="sortasc-header" SortedDescendingHeaderStyle-CssClass="sortdesc-header" CommandRowStyle-CssClass="command" PagerStyle-CssClass="page" OnRowUpdating="gvHolidays_RowUpdating"> <AlternatingRowStyle CssClass="alternating" /> <Columns> <asp:CommandField ShowEditButton="true" ValidationGroup="EditValidationControls" /> <asp:BoundField DataField="HolidayID" HeaderText="HolidayID" InsertVisible="False" ReadOnly="True" SortExpression="HolidayID" Visible="False" ShowHeader="False" /> <asp:BoundField DataField="HStartDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Start Date" SortExpression="HStartDate" /> <asp:BoundField DataField="HEndDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="End Date" SortExpression="HEndDate" /> <asp:BoundField DataField="HDescription" HeaderText="Description" SortExpression="HDescription" /> </Columns> <HeaderStyle CssClass="header" /> <PagerStyle CssClass="page" /> <SortedAscendingHeaderStyle CssClass="sortasc-header" /> <SortedDescendingHeaderStyle CssClass="sortdesc-header" /> </asp:GridView> <asp:Label ID="lblNullUpdate" runat="server" Font-Bold="True" Font-Size="Large" ForeColor="Red" Text="Label" Visible="False"></asp:Label> <asp:SqlDataSource ID="HolidaysDS" runat="server" OnInserted="HolidaysDS_Inserted" OnUpdated="HolidaysDS_Updated" ConnectionString="<%$ ConnectionStrings:TigerTimeTrackerConnectionString %>" ProviderName="<%$ ConnectionStrings:TigerTimeTracker_ConnectionString.ProviderName %>" InsertCommand="usp_Holidays_Add_Holiday" InsertCommandType="StoredProcedure" SelectCommand="usp_Holidays_Get_Holidays_By_Year" SelectCommandType="StoredProcedure" UpdateCommand="usp_Holidays_Update_Holiday" UpdateCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="ddlHolidayYears" Name="Year" PropertyName="SelectedValue" Type="String" /> </SelectParameters> <UpdateParameters> </UpdateParameters> <InsertParameters> </InsertParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="HolidayYearDS" runat="server" ConnectionString="<%$ ConnectionStrings:TigerTimeTracker_ConnectionString %>" ProviderName="<%$ ConnectionStrings:TigerTimeTracker_ConnectionString.ProviderName %>" SelectCommand="usp_Holidays_Get_Distinct_Holiday_Years" SelectCommandType="StoredProcedure"> </asp:SqlDataSource> <asp:DetailsView ID="dvHolidays" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="HolidayID" DataSourceID="HolidaysDS" DefaultMode="Insert" CssClass="dView" HeaderStyle-CssClass="header" FieldHeaderStyle-CssClass="fieldHeader" AlternatingRowStyle-CssClass="alternating" CommandRowStyle-CssClass="command" PagerStyle-CssClass="page" FooterStyle-CssClass="footer" OnItemInserted="dvHolidays_ItemInserted" OnItemInserting="dvHolidays_ItemInserting"> <AlternatingRowStyle CssClass="alternating" /> <CommandRowStyle CssClass="command" /> <FieldHeaderStyle CssClass="fieldHeader" /> <Fields> <asp:BoundField DataField="HolidayID" HeaderText="HolidayID" InsertVisible="False" ReadOnly="True" SortExpression="HolidayID" /> <InsertItemTemplate> '></asp:TextBox> <asp:RequiredFieldValidator ID="YearRequiredValidator" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayYear" ErrorMessage="Required"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="YearLengthValidator" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayYear" ValidationExpression="^[0-9]{4,4}$" ErrorMessage="The Year can only be numeric(0-9), and has to be four digits."></asp:RegularExpressionValidator> </InsertItemTemplate> </asp:TemplateField> <EditItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayStartEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayStartEdit" ErrorMessage="Required"></asp:RequiredFieldValidator> </EditItemTemplate> <InsertItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayStartInsertRequiredValidator" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayStartInsert" ErrorMessage="Required"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> <EditItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayEndEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayEndEdit" ErrorMessage="Required"></asp:RequiredFieldValidator> <asp:CompareValidator ID="HolidayRangeCheckEdit" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayEndEdit" ErrorMessage="End Date must be after Start Date." ControlToCompare="txtHolidayStartEdit" Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator> </EditItemTemplate> <InsertItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayEndInsertRequiredValidator" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayEndInsert" ErrorMessage="Required"></asp:RequiredFieldValidator> <asp:CompareValidator ID="HolidayRangeCheckInsert" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayEndInsert" ControlToCompare="txtHolidayStartInsert" Operator="GreaterThanEqual" Type="Date" ErrorMessage="Please enter an End Date that is after the Start Date."></asp:CompareValidator> </InsertItemTemplate> </asp:TemplateField> <EditItemTemplate> '></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> '></asp:TextBox> </InsertItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="true" ValidationGroup="InsertValidationControls"/> </Fields> <FooterStyle CssClass="footer" /> <HeaderStyle CssClass="header" /> <PagerStyle CssClass="page" /> </asp:DetailsView> <asp:Label ID="lblInsertUniqueError" runat="server" Font-Bold="True" Font-Size="Large" ForeColor="Red" Text="Label" Visible="False"></asp:Label> </asp:View> |
一直以来的问题是我在 gridview 中使用绑定字段而不是模板字段。这是我为遇到此问题的其他人更新的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <asp:GridView ID="gv_Holidays" runat="server" AutoGenerateColumns="False" DataKeyNames="HolidayID" DataSourceID="HolidaysDS" AllowSorting="True" CssClass="gvhours" HeaderStyle-CssClass="header" AlternatingRowStyle-CssClass="alternating" SortedAscendingHeaderStyle-CssClass="sortasc-header" SortedDescendingHeaderStyle-CssClass="sortdesc-header" CommandRowStyle-CssClass="command" PagerStyle-CssClass="page" OnRowUpdating="gvHolidays_RowUpdating"> <AlternatingRowStyle CssClass="alternating" /> <Columns> <asp:CommandField ShowEditButton="true" ValidationGroup="EditValidationControls" /> <asp:BoundField DataField="HolidayID" HeaderText="HolidayID" InsertVisible="False" ReadOnly="True" SortExpression="HolidayID" Visible="False" ShowHeader="False" /> <EditItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayStartEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayStartEdit" ErrorMessage="Required"></asp:RequiredFieldValidator> </EditItemTemplate> <ItemTemplate> ' ></asp:Label> </ItemTemplate> </asp:TemplateField> <EditItemTemplate> ' ClientIDMode="Static"></asp:TextBox> <asp:RequiredFieldValidator ID="HolidayEndEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayEndEdit" ErrorMessage="Required"></asp:RequiredFieldValidator> <asp:CompareValidator ID="HolidayRangeCheckEdit" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayEndEdit" ErrorMessage="End Date must be after Start Date." ControlToCompare="txtHolidayStartEdit" Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator> </EditItemTemplate> <ItemTemplate> '></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="HDescription" HeaderText="Description" SortExpression="HDescription" /> </Columns> <HeaderStyle CssClass="header" /> <PagerStyle CssClass="page" /> <SortedAscendingHeaderStyle CssClass="sortasc-header" /> <SortedDescendingHeaderStyle CssClass="sortdesc-header" /> </asp:GridView> |