当asp.net编辑模板验证控件运行时,c# IsVaild不会翻转为False,应该返回False

本文关键字:False 翻转 asp 返回 IsVaild 编辑 验证 控件 运行时 net | 更新日期: 2023-09-27 18:10:27

我有一个为我们的人力资源系统跟踪公司假期的页面,但是现在,如果您尝试编辑假期的结束日期,并且用户不小心输入了在开始日期之前的结束日期,它仍然验证为真,并允许将编辑写入数据库表。

我错过了什么,使页面总是验证为True?我在这个页面上也有一个DetailsView,它使用了一组验证控件,它正在工作,当某些东西没有通过验证时,它将验证为false。

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;
}

这是我当前的c#处理异常,如果用户在更新中传递null,以及应该验证页面并检查是否所有内容都有效的事件。

<h2>Holidays</h2>
        <asp:Label ID="lblHolidayYear" runat="server" Text="Holiday Year:" AssociatedControlID="ddlHolidayYears"></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="HYear" HeaderText="Year" SortExpression="HYear" Visible="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> 
                   <asp:Parameter Name="HolidayID" Type="Int32"  />
                   <asp:ControlParameter ControlID="ddlHolidayYears" Name="HYear" PropertyName="SelectedValue" Type="String" />
                   <asp:Parameter Name="HStartDate" Type="DateTime" />
                   <asp:Parameter Name="HEndDate" Type="DateTime" />
                   <asp:Parameter Name="HDescription" Type="String" />
               </UpdateParameters>
               <InsertParameters>
                   <asp:Parameter Name="Year" Type="String" />
                   <asp:Parameter Name="StartDate" Type="DateTime" />
                   <asp:Parameter Name="EndDate" Type="DateTime" />
                   <asp:Parameter Name="Description" Type="String" />
               </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" />
                <asp:TemplateField HeaderText="Year: " SortExpression="HYear">
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtHolidayYear" runat="server" Text='<%# Bind("Year") %>'></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>
                <asp:TemplateField HeaderText="Start Date: " SortExpression="HStartDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHolidayStartEdit" runat="server" Text='<%# Bind("HStartDate") %>' ClientIDMode="Static"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="HolidayStartEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayStartEdit"
                            ErrorMessage="Required"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtHolidayStartInsert" runat="server" Text='<%# Bind("StartDate") %>' ClientIDMode="Static"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="HolidayStartInsertRequiredValidator" runat="server" ValidationGroup="InsertValidationControls" ControlToValidate="txtHolidayStartInsert"
                            ErrorMessage="Required"></asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="End Date: " SortExpression="HEndDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHolidayEndEdit" runat="server" Text='<%# Bind("HEndDate") %>' 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>
                        <asp:TextBox  ID="txtHolidayEndInsert" runat="server" Text='<%# Bind("EndDate") %>' 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>
                <asp:TemplateField HeaderText="Description: " SortExpression="HDescription">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHolidayDescriptionEdit" runat="server" Text='<%# Bind("HDescription") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtHolidayDescriptionInsert" runat="server" Text='<%# Bind("Description") %>'></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>

当asp.net编辑模板验证控件运行时,c# IsVaild不会翻转为False,应该返回False

问题是我在gridview中使用绑定字段而不是模板字段。这里是我更新的代码,为任何人谁遇到这个问题。

<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="HYear" HeaderText="Year" SortExpression="HYear" Visible="false" />
                <asp:TemplateField HeaderText="Start Date" SortExpression="HStartDate" >
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHolidayStartEdit" runat="server" Text='<%# Bind("HStartDate", "{0:MM/dd/yyyy}") %>' ClientIDMode="Static"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="HolidayStartEditRequiredValidator" runat="server" ValidationGroup="EditValidationControls" ControlToValidate="txtHolidayStartEdit"
                            ErrorMessage="Required"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblStartItem" runat="server" Text='<%# Bind("HStartDate", "{0:MM/dd/yyyy}") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="End Date" SortExpression="HEndDate" >
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHolidayEndEdit" runat="server" Text='<%# Bind("HEndDate", "{0:MM/dd/yyyy}") %>' 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 ID="lblEndItem" runat="server" Text='<%# Bind("HEndDate", "{0:MM/dd/yyyy}") %>'></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>