Strange behavior with Calendar binding

Last post 04-29-2008 5:59 PM by CaptainStarbuck. 3 replies.

Sort Posts:

  • Strange behavior with Calendar binding

    01-25-2006, 1:07 PM
    • Loading...
    • jcasp
    • Joined on 04-10-2003, 10:43 AM
    • Posts 2,286

    I've seen many posts where people are confused about the Calendar control not binding properly with the underlying data source.  The symptoms are either that the value is not updated or that it's updated with the 1st day of the month selected.

    The problem has to do with misuse of Bind() versus Eval() for certain Calendar properties.  For example, consider the following common mistake:

    <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("DateField") %>'

    VisibleDate='<%# Bind("DateField") %>'></asp:Calendar>

    Experienced developers will immediately detect the problem here.  SelectedDate is properly defined using the Bind() method, but the VisibleDate should NOT be using Bind(), but Eval() instead as follows:

    <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("DateField") %>'

    VisibleDate='<%# Eval("DateField") %>'></asp:Calendar>

    The VisibleDate property defines the default current Month that should be display for the Calendar (note that the date portion is irrelevant here).  If Bind() was used instead of Eval() for this value, then it can really throw you off with a strange binding behavior.  For example, if you select a different date from the current month being shown, then the change is NOT update to the table.  This is because the VisibleDate property value ignores the date portion and the internal processing thinks the value did not change.  However, if you select a date from a different month, then the first date of that month will be updated.  As you might have guessed, this is because the month portion of the value changed, so the change in value is detected and saved, but with the 1st date of that month.  All this happens while the true binding for the SelectedDate is ignored due to another "binding" for the VisibleDate property.

    So, rather than scratching your head and searching for solutions, becareful not to bind the VisibleDate property, but use the proper Eval() method instead.

  • Re: Strange behavior with Calendar binding

    01-25-2006, 1:38 PM
    Just to add to this thread. Here is why I don't use Eval() in .NET 2.0: What's wrong with Eval()?
    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: Strange behavior with Calendar binding

    01-28-2008, 6:54 AM
    • Loading...
    • sam_soumya
    • Joined on 01-10-2008, 11:42 AM
    • Posts 76

     Hi,

    I think Eval() is used used, if we want to do any operation upon that value. Like <GridViwe: ......  commandargument=<%#Eval("Id")%> commandname=<%#Bind("Name")%>

    Thanks & Regards.
    Soumya
  • Re: Strange behavior with Calendar binding

    04-29-2008, 5:59 PM

    Ryan's link is a 404.
    I chased down his blog to http://ryanolshan.com/technology/what-s-wrong-with-eval/

Page 1 of 1 (4 items)