gpt4 book ai didi

asp.net - 如何使用比较验证器验证时间?

转载 作者:行者123 更新时间:2023-12-02 13:10:21 28 4
gpt4 key购买 nike

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

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

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

最佳答案

你可以试试这个

在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

关于asp.net - 如何使用比较验证器验证时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19999940/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com