3

日付をテキストボックスに挿入して、その日付をカレンダーで選択できるかどうかを知る必要があります。Microsoft Visual Studio Express 2012 for web でカレンダーを使用しています。

以下は、カレンダーで日付を選択してテキストボックスに日付を挿入するコードです。(でも逆にしたい)

デフォルト.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

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;

}
}

ありがとう

4

3 に答える 3

5

どうですか

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
}
于 2013-11-05T11:22:02.210 に答える
0

テキストが変更されたときのテキストボックスにイベントを作成します

イベント内で、テキストが例外を処理するための実際の日時であるかどうかを確認します

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);
  }
}
于 2013-11-05T11:36:06.717 に答える