4

如何使用比较验证器验证时间?

我有一个可编辑的网格视图,其中包含 2 列时间开始和结束时间,我想验证它们的开始时间必须早于结束时间。我有 24 小时制。

例如,如果我输入开始时间:20:30,结束时间是 19:30,它应该给我错误并阻止我更新行

4

1 回答 1

1

你可以试试这个

在 aspx 代码中

<div>
    <asp:GridView ID="gvDetails" DataKeyNames="EventID,EventName" runat="server" AutoGenerateColumns="false"
        CssClass="Gridview" HeaderStyle-BackColor="#61A6F8" ShowFooter="true" HeaderStyle-Font-Bold="true"
        HeaderStyle-ForeColor="White" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
        OnRowDeleting="gvDetails_RowDeleting" OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating"
        OnRowCommand="gvDetails_RowCommand">
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
                        ToolTip="Update" Height="20px" Width="20px" />
                    <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
                        ToolTip="Cancel" Height="20px" Width="20px" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg"
                        ToolTip="Edit" Height="20px" Width="20px" />
                    <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server"
                        ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg"
                        CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EventName">
                <EditItemTemplate>
                    <asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("EventName") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("EventName") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrusrname" runat="server" />
                    <asp:RequiredFieldValidator ID="rfvEventName" runat="server" ControlToValidate="txtftrusrname"
                        Text="*" ValidationGroup="validaiton" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="StartTime">
                <EditItemTemplate>
                    <asp:TextBox ID="txtStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrStartTime" runat="server" />
                    <asp:RequiredFieldValidator ID="rfvStartTime" runat="server" ControlToValidate="txtftrStartTime"
                        Text="*" ValidationGroup="validaiton" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EndTime">
                <EditItemTemplate>
                    <asp:TextBox ID="txtstate" runat="server" Text='<%#Eval("EndTime") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblstate" runat="server" Text='<%#Eval("EndTime") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrEndTime" runat="server" />
                    <asp:RequiredFieldValidator ID="rfvEndTime" runat="server" ControlToValidate="txtftrEndTime"
                        Text="*" ValidationGroup="validaiton" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

在代码后面

protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
                TextBox txtStartTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrStartTime");
                TextBox txtEndTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrEndTime");
                if (Convert.ToDateTime(txtEndTime.Text.Trim().ToString()) <= Convert.ToDateTime(txtStartTime.Text.Trim().ToString()))
                {
                    lblresult.Text = "Please enter valid end time";
                }
                else
                {
                    con.Open();
                    SqlCommand cmd =
                        new SqlCommand(
                            "insert into tblEvents(EventName,StartTime,EndTime) values('" + txtUsrname.Text + "','" +
                            txtStartTime.Text + "','" + txtEndTime.Text + "')", con);
                    int result = cmd.ExecuteNonQuery();
                    con.Close();
                    if (result == 1)
                    {
                        BindEmployeeDetails();
                        lblresult.ForeColor = Color.Green;
                        lblresult.Text = txtUsrname.Text + " Details inserted successfully";
                    }
                    else
                    {
                        lblresult.ForeColor = Color.Red;
                        lblresult.Text = txtUsrname.Text + " Details not inserted";
                    }


                }

            }
        }

在此处输入图像描述

于 2014-01-24T07:27:54.863 回答