On my form I have a TextBox control with the TextMode property set to 'Date' and (for pleteness) the normal date format for my region is 'dd/MM/yyyy' (UK):
<asp:TextBox ID="tbFrom" runat="server" TextMode="Date" AutoPostBack="True" OnTextChanged="DatesChanged"></asp:TextBox>
When the user changes the date I want to do something in the code behind hence the AutoPostback and OnTextChanged property settings.
Now this works fine when the user uses the calendar pop-up control to pick a date but when the user enters the date manually, the control is posting back before the date has been pletely entered as required.
For example, the user enters '01' for day, '01' for month and then starts entering '2017' for the year. As soon as the user types '2' in the year part, the TextChanged event is triggered, the control posts back with the (perfectly valid) date '01/01/0002' and the control then loses focus.
I understand that, technically, this is working as it should but it has the effect of making it impossible for a user to plete the required date unless they enter the various sections in a very specific way (year, day, month for instance).
My direct question would be 'is it possible to disable manual date entry for this control?'. However, I would also wele any suggestions on how to retain the manual entry but handle the postback in a way that allows a user to enter the full date first.
Many thanks.
On my form I have a TextBox control with the TextMode property set to 'Date' and (for pleteness) the normal date format for my region is 'dd/MM/yyyy' (UK):
<asp:TextBox ID="tbFrom" runat="server" TextMode="Date" AutoPostBack="True" OnTextChanged="DatesChanged"></asp:TextBox>
When the user changes the date I want to do something in the code behind hence the AutoPostback and OnTextChanged property settings.
Now this works fine when the user uses the calendar pop-up control to pick a date but when the user enters the date manually, the control is posting back before the date has been pletely entered as required.
For example, the user enters '01' for day, '01' for month and then starts entering '2017' for the year. As soon as the user types '2' in the year part, the TextChanged event is triggered, the control posts back with the (perfectly valid) date '01/01/0002' and the control then loses focus.
I understand that, technically, this is working as it should but it has the effect of making it impossible for a user to plete the required date unless they enter the various sections in a very specific way (year, day, month for instance).
My direct question would be 'is it possible to disable manual date entry for this control?'. However, I would also wele any suggestions on how to retain the manual entry but handle the postback in a way that allows a user to enter the full date first.
Many thanks.
Share Improve this question edited May 2, 2017 at 11:35 Tummala Krishna Kishore 8,2714 gold badges34 silver badges51 bronze badges asked May 2, 2017 at 11:25 John PJohn P 3491 gold badge4 silver badges19 bronze badges2 Answers
Reset to default 3Yes, this can be done , by using
onkeypress="return false;" onpaste="return false"
Add them to your textbox markup as follows
<asp:TextBox ID="tbFrom" runat="server" onkeypress="return false;"
onpaste="return false" TextMode="Date" AutoPostBack="True"
OnTextChanged="DatesChanged"></asp:TextBox>
I found a solution that works using the built in TextMode="Date". Instead of using AutoPostBack="true" to post back after a change was made, use the onBlur event.
<asp:TextBox ID="tbFrom" runat="server" TextMode="Date"
OnTextChanged="DatesChanged" />
In the page load add:
tbFrom.Attributes.Add("onBlur", "__doPostBack('tbFrom','');")
It will trigger the regular OnTextChanged event for the textbox.