3

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.

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

<%@ 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;

}
}

Thanks

user2855068
  • 43
  • 1
  • 1
  • 5
  • You can use the same property `SelectedDate` to specify which date should be selected. What is the problem, do you need it on clientside or do you have problems with parsing the date? – Tim Schmelter Nov 05 '13 at 11:21

3 Answers3

5

How about

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
}
Tobberoth
  • 9,327
  • 2
  • 19
  • 17
  • 1
    Worth to mention that is is rather important to have a validator on the `textbox`, and a check for validity before amending the `SelectedDate`, so there won't be any nasty errors showing up. – Amber Nov 05 '13 at 11:31
2
 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.

Community
  • 1
  • 1
Kuldeep
  • 454
  • 1
  • 3
  • 11
0

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);
  }
}
Schuere
  • 1,579
  • 19
  • 33