Error Calendar control in EditItemTemplate

Hello Guys,

I try to use Calendar control with GridView. Everything is fine, except one case. One column in database is "date" type, so for edit I would like to use asp:Calendar control.

And if the cell stores date following code in  <EditItemTemplate> works perfect:

<asp:Calendar ID="CalendarRECEIVING_DATE" runat="server"  SelectedDate='<%# Bind("RECEIVING_DATE")   %>'  ></asp:Calendar>

But if cell is empty I am getting error "System.InvalidCastException: Specified cast is not valid.". If I use Eval instead of Bind it works, but I have to find solution to give a user possibility to enter the date and save it to the database. And even I can't use checking against null values like this:

<asp:Calendar ID="CalendarRECEIVING_DATE" runat="server" SelectedDate='<%# string.IsNullOrEmpty(Bind("RECEIVING_DATE").ToString()) ? "0000-00-00 00:00:00.000" : Bind("RECEIVING_DATE") %>'  ></asp:Calendar>

as I get an error "The name 'Bind' does not exist in the current context".

Do you have any clues? I was trying many solution and no effect.

Regards

Tomasz

Try setting it to nothing:

        

<asp:Calendar ID="CalendarRECEIVING_DATE" runat="server" SelectedDate='<%# String.IsNullOrEmpty(Bind("RECEIVING_DATE")) : Bind(Nothing)%>'  ></asp:Calendar>

It doesn't work, still getting "The name 'Bind' does not exist in the current context".

Then let's try taking it out:

<asp:Calendar ID="CalendarRECEIVING_DATE" runat="server" SelectedDate='<%# String.IsNullOrEmpty(Bind("RECEIVING_DATE")) : Nothing%>'  ></asp:Calendar>

hi instead of trying the above code plz try this

DateTime tomorrow = DateTime.Today.AddDays(1);
Calendar1.TodaysDate = tomorrow;
Calendar1.SelectedDate = Calendar1.TodaysDate;

check the link for detail

http://msdn.microsoft.com/en-us/library/8k0f6h1h(v=vs.85).aspx

But I am not sure if it's the case. I am using <EditItemTemplate>, so one problem is to set asp:Calendar control to any value (for example current date), but the second is to send back asp:Calendar value to database on GridView update. I don't know how your code can solve my problem?

Thanks

1 thoughts on "Error Calendar control in EditItemTemplate"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>