0

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

我错过了什么使页面始终验证为 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;
}

如果用户在更新中传递 null 以及应该验证页面并检查一切是否有效的事件,那是我当前的 C# 处理异常。

<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>
4

1 回答 1

1

整个时间的问题是我在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>
于 2013-04-23T20:16:18.463 回答