Page_Loaded event seems to be the wrong place to put in code for doing calculations using controls on the page

5 posts, 0 answers
  1. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 27 Sep 2012 Link to this post

    I've got 2 controls on a page, one for a request date and another for the date of birth of a client.  Both of these values come from a SQL Server database.  During the Page_Loaded event I had been trying to calculate the age of the client on the request date, however that's not working.  For some reason, which I don't understand, the date of birth of the client is wrong (it turns out to be today's date), during the Page_Loaded event.  However, once the page is actually displayed, it is correct.  I had thought Page_Loaded would have been the correct event to put this code, but that doesn't seem to be the case.
     
    So, where should I put the age calculation code?  In what event?

    The XAML for the first control is:
    <telerik:RadMaskedDateTimeInput Name="radReqDate" MinWidth="80" TabIndex="0" FontWeight="Bold"
                                   Value="{Binding RequestDate}" InputBehavior="Replace" TextMode="PlainText"
                                   SelectionOnFocus="SelectAll" ValueChanged="radReqDate_ValueChanged" />

    and the XAML for the second control is here:
    <telerik:RadMaskedDateTimeInput Name="radDOB" EmptyContent="Enter DOB" MinWidth="80" TabIndex="14"
                                   FontWeight="Bold" InputBehavior="Replace" TextMode="PlainText"
                                   Value="{Binding Path=DateOfBirth, Mode=TwoWay}" ValueChanged="radDOB_ValueChanged" />

  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 02 Oct 2012 Link to this post

    Hi Rod,

    From your description I gather that the RadMaskedDateTimeInput values are still not updated in the Loaded event handler of the page. However, as I am not sure how your layout is built, I am not sure what is causing the delay in the value update or if this is the cause for the issue.

    This is why I prepared a sample solution to test your scenario and I wasn't able to reproduce the case. Can you please take a look at the project and let me know if I'm missing something? Also, you can use the data objects to calculate the age of the client - basically instead of using the Value property of the MaskedInput controls, you can calculate the age as this:
           var age = radReqDate.Year - radDOB.Year;

    All the best,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 02 Oct 2012 Link to this post

    Hello Tina,

    Thank you for your reply.  I don't have the Telerik controls 2012.2.912.40.  The last I had was 2011 Q2, I believe.  (I can't open your project without errors.)
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 05 Oct 2012 Link to this post

    Hi Rod,

    I tested my solution with the Q2 2011 assemblies and I am still not able to reproduce the issue. Also, on our side the project can be correctly downloaded and it runs without any issues and this is why I am not sure why you're experiencing issues with it.

    Can you try the solution I attached to this post - it uses the Q2 2011 dlls and it still claculates the age correctly. Let me know if I'm missing something.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 05 Oct 2012 Link to this post

    WOW, you're right.  You're example worked fine.  I'm floored; don't know why your's does and mine doesn't.  (I'm getting an odd error in the VS 2010 designer when I open your project; something about not being able to load the file or assembly 'Telerik.Windows.Controls, Version=2011.2.712.40, but it didn't stop me from running it, and of course I can look through the XAML.)
Back to Top
UI for WPF is Visual Studio 2017 Ready