将日期插入文本框中,然后将在日历中选择该日期 asp.net c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19787747/
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
Insert a date into a textbox and then that date would be selected in the calendar asp.net c#
提问by user2855068
I need to know is it possible to insert a date into a textbox and then that date would be selected in the calendar. I am using the calendar in Microsoft visual studio express 2012 for web.
我需要知道是否可以在文本框中插入日期,然后在日历中选择该日期。我在 Microsoft Visual Studio Express 2012 for web 中使用日历。
Below is the code for inserting a date into a textbox by selecting the date on the calendar. (However I want to do the opposite)
下面是通过选择日历上的日期将日期插入文本框的代码。(但是我想做相反的事情)
Default.aspx
默认.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" Visible="False" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">PickDate...</asp:LinkButton>
</form>
Default.aspx.cs
默认.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToLongDateString();
Calendar1.Visible = false;
}
}
Thanks
谢谢
采纳答案by Tobberoth
How about
怎么样
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
}
回答by Kuldeep
protected void LinkButton1_Click(object sender, EventArgs e)
{
if(TextBox1.Text!=string.Empty)
Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
Calendar1.Visible = true;
}
But you need to validate user eneterd datetime before converting textbox value to datetime. Here is the link of javascript datetime validation.
回答by Schuere
create an event on the Textbox for when the text is changing
在文本更改时在文本框上创建一个事件
Inside the event check wether the text is an actual datetime for handling exceptions
在事件检查中,文本是否是用于处理异常的实际日期时间
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DateTime curDate = DateTime.Now;
if (DateTime.TryParse(TextBox1.Text, out curDate))
{
Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
}
}