C# ASP.NET 日期和时间选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:19:34  来源:igfitidea点击:

ASP.NET date and time picker?

c#asp.net

提问by Etienne

I am using ASP.NET 2.0 with SQL Server 2005. I want my user to select a date and time and then save those values into the database. In VS I can use the Calendar control to get the date, but what is a good one for handeling the date the user select plus the time what ever it might be that the user must also select from a control.

我在 SQL Server 2005 中使用 ASP.NET 2.0。我希望我的用户选择一个日期和时间,然后将这些值保存到数据库中。在 VS 中,我可以使用 Calendar 控件来获取日期,但是对于处理用户选择的日期以及用户还必须从控件中选择的时间来说,有什么好的方法。

Thanks in advance!!

提前致谢!!

采纳答案by David

Here's a nice free control: http://www.asp.net/community/control-gallery/item.aspx?i=535

这是一个不错的免费控件:http: //www.asp.net/community/control-gallery/item.aspx?i=535

And another (so you have a choice)

另一个(所以你有选择)

http://www.asp.net/community/control-gallery/item.aspx?i=3221

http://www.asp.net/community/control-gallery/item.aspx?i=3221

Although to be perfectly honest, I normally just use drop-down lists and combine them in code-behind. (Although this could be wrapped in a user control fairly easily.)

尽管老实说,我通常只使用下拉列表并将它们组合在代码隐藏中。(虽然这可以很容易地包装在用户控件中。)

ASPX:

ASPX:

<asp:TextBox ID="txtTime" runat="server" Width="90px"></asp:TextBox>
<asp:DropDownList ID="ddlAmPm" runat="server">
   <asp:ListItem Selected="True">AM</asp:ListItem>
   <asp:ListItem Selected="False">PM</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtTime" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="TimeValidator" runat="server" ControlToValidate="txtTime" Display="Dynamic" ErrorMessage="Invalid Time.  Enter time in a valid format.  Example: 12:30 or 5:00" ValidationExpression="^(1[0-2]|[1-9]):[0-5][0-9]$" EnableClientScript="False"></asp:RegularExpressionValidator>

VB Code-behind:

VB 代码隐藏:

 Dim strDateTime As String = txtDate.Text & " " & txtTime.Text & " " & ddlAmPm.SelectedItem.Value

回答by Jarret Raim

You can use jQuery controls to provide the DateTime picker and just have them dump the selected DateTime into a textbox that the rest of the app can use. Won't work for non-Javascript cases, but otherwise it should be okay.

您可以使用 jQuery 控件来提供 DateTime 选择器,并让它们将选定的 DateTime 转储到应用程序的其余部分可以使用的文本框中。不适用于非 Javascript 的情况,否则应该没问题。

回答by Jakkwylde

I'd go with the JQuery UI datepicker. You can customize the date format and language to fit your need and perform actions like restrict the selectable date ranges and add in buttons and other navigation options. In addition to this there are a variety of keyboard shortcuts that are quite nice when you get used to them.

我会使用 JQuery UI 日期选择器。您可以自定义日期格式和语言以满足您的需要,并执行诸如限制可选日期范围以及添加按钮和其他导航选项等操作。除此之外,还有各种键盘快捷键,当您习惯它们时会非常好。

http://jqueryui.com/demos/datepicker/

http://jqueryui.com/demos/datepicker/

Used to use both ASP.net AJAX calendar control but would often have CSS formatting issues and weird cases with client side required field validators not working properly. Since then, the jquery datepicker has proven to be a better fit in instances where I use it.

用于同时使用 ASP.net AJAX 日历控件,但通常会出现 CSS 格式问题和客户端所需字段验证器无法正常工作的奇怪情况。从那时起,jquery datepicker 被证明更适合我使用它的情况。

You can find a good tutorial on how to use the jquery datepicker it here:

您可以在此处找到有关如何使用 jquery datepicker 的好教程:

http://elegantcode.com/2008/05/06/using-jquery-datepicker-in-aspnet/

http://elegantcode.com/2008/05/06/using-jquery-datepicker-in-aspnet/

回答by Pragnesh Patel

Check out this cool Jquery Date Time Picker.

看看这个很酷的Jquery 日期时间选择器

回答by Pragnesh Patel

I've found the calender extender from the asp.net ajax toolkit to be very easy to use:

我发现来自 asp.net ajax 工具包的日历扩展器非常易于使用:

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx