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

时间:2013-11-15 11:22:01

标签: asp.net gridview time comparevalidator

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

我有一个可编辑的gridview,包含2列时间Start&结束时间,我想验证他们明星时间必须早于结束时间。我有24小时格式。

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

1 个答案:

答案 0 :(得分: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";
                    }


                }

            }
        }

enter image description here