Calendar control in asp.net c#
I have a text box and a calendar in my ASP.NET web application.
When I select any date in calendar, I would like the d开发者_开发知识库ate/month/year of that date to be displayed in the text box.
in .aspx file
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
</asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
</form>
in .aspx.cs file
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString();
}
Always use google before you ask question : http://www.google.co.in/search?q=asp.net+%2B+calander+control+%2B+textbox&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
check the answer below
private void Calendar1_SelectionChanged(System.Object sender, System.EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate;
}
or
use OnClientDateSelectionChanged
. Similar example explained here well
CalendarExtender Change date with Javascript
or
Calendar Demonstration
Assuming you already use the onselectionchanged event but not seeing result directly you could use a updatepanel like this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>
If you were just looking for the event then it would look alike this
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar.cal.SelectedDate.ToString();
}
You could also try looking at the Ajax Toolkit CalendarExtender. This gives you a text box that when you click in it opens a calendar and the selected date is automatically added to the text box.
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/calendar/calendar.aspx
Handle the "SelectionChanged" Event of the calender control and inside the event write this code,
txtbox.Text = Calendar1.SelectedDate;
txtbox.Invalidate();
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>
精彩评论