I have an asp.net page with a an html5 TextBox control set as the “date” type. The control on my page looks like this:
<asp:TextBox ID="TextBoxMyDate" runat="server" type="date"/>
It works great in the page and I can click on it and set the date. I can also read the date through TextBoxMyDate.Text. However, I cannot figure out how to programaticaly set the date. I have tried various formats of:
TextBoxMyDate.Text = DateTime.UtcNow.ToString("MM/dd/yyyy")
TextBoxMyDate.Text = DateTime.UtcNow.ToString("MM/dd/yyyy")
My guess is that its much more complicated than simply setting the text value but I don’t know where to go from here. Any suggestions?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This is actually the correct way to set the date but the format was wrong!
TextBoxMyDate.Text = DateTime.UtcNow.ToString("yyyy-MM-dd");
I was confusing how the browser displays the date and the html5 standards for this control!
Method 2
Yes, it is quite possible. Here is an example of a date selection filter with ajaxcontrotoolkit and the textbox field.
Overview:calendar ajaxControlToolkit Overview
Tutorial:Tutorial ajaxControlTollkit Date textbox
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<asp:CalendarExtender
ID="CalendarExtender1"
TargetControlID="txtStartDate"
runat="server" />
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0