0

i have run into a problem where I have textbox with a calenderextender from ajaxtoolkit. I would like to retrieve the value the user puts in. This is how my code looks like

<asp:TextBox ID="txtValidFrom" runat="server" TextMode="DateTime" AutoPostBack="true" ></asp:TextBox>
<asp:TextBox ID="txtValidTo" runat="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="exValidFrom" runat="server" TargetControlID="txtValidFrom" FirstDayOfWeek="Monday" Format="dd/MM/yyyy"></ajaxToolkit:CalendarExtender>
<ajaxToolkit:CalendarExtender ID="exValidTo" runat="server" TargetControlID="txtValidTo" FirstDayOfWeek="Monday" Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>

And my code behind:

logg.ValidFrom = exValidFrom.SelectedDate;
logg.ValidTo = exValidTo.SelectedDate;

Where ValidFrom and ValidTo are DateTime? variables. No matter what the user presses i always get null as value. I have tried to take the text direct from the textbox using

logg.ValidFrom = System.Convert.ToDateTime(txtValidFrom.Text);
logg.ValidTo = System.Convert.ToDateTime(txtValidTo.Text);

But i get the error

String was not recognized as a valid DateTime.

Any ideas how to solve this?

Adnan Hossain
  • 117
  • 2
  • 13
  • Have you tried to see which value the CalendarExtender sets when the user selects a date? It looks like it is not setting the textboxes date. Maybe you can assign `$('#txtValidFrom').val([whateverselectedwhereeverselected])` on select of the CalenderExtender? – Naz Ekin Jun 28 '16 at 09:48
  • How do i assign that? The textbox is filled with the selecteddate. Is there any other way to see what the calenderextender sets? @biseibutsu – Adnan Hossain Jun 28 '16 at 09:49
  • I meant to say see if the textbox value is set from the browser developer console, you can set the value if it is not setting with Javascript. – Naz Ekin Jun 28 '16 at 09:50
  • And how do i set that? @biseibutsu – Adnan Hossain Jun 28 '16 at 09:53
  • I don't know where you see the value, so I am not sure exactly? But for example if you find the value you want the textbox to be set, you can do `$('[id*=txtValidFrom]').val(value);` obviously you need to include jquery for that and I can not say where to get value from, because you haven't shared the rendered html. – Naz Ekin Jun 28 '16 at 09:58
  • I am a little bit confused. The code behind is C# so i'm not familiar with Jquery or javascript... @biseibutsu – Adnan Hossain Jun 28 '16 at 10:02
  • Maybe you should check the documentation for CalenderExtender then, I don't know which property will give you the value. – Naz Ekin Jun 28 '16 at 10:05

0 Answers0