0

I want to calculate an Age inside a repeater with an existing field.

Query to populate repeater:

var qryGetAllBeneficiaries = from p in dbRRSP.Person 
         join rt in dbRRSP.RelationshipType on p.RelationshipTypeId equals rt.RelationshipTypeId
           where p.PlanId == qryPersonDetails.PlanId
           orderby p.LastName
         select new
               {
             BeneficiaryLastName = p.LastName, 
             BeneficiaryFirstName = p.FirstName,
             BeneficiaryMiddleName = p.MiddleName,
             BeneficiaryAka = p.Aka,
             BeneficiaryBirthday = p.Birthdate,
             BeneficiaryRelationshipToClient = rt.RelationshipTypeDescription
                            };

            rptBeneficiary.DataSource = qryGetAllBeneficiaries;
            rptBeneficiary.DataBind();

Repeater itself:

<asp:Repeater ID="rptBeneficiary" runat="server">
    <HeaderTemplate>
    <table>      
    <tr>
    <td class="labels displayInput_noWidth">Last Name</td>
    <td class="labels displayInput_noWidth">First Name</td>
    <td class="labels displayInput_noWidth">Middle Name</td>
    <td class="labels displayInput_noWidth">Aka</td>
    <td class="labels displayInput_75w">Birthday</td>
    <td class="labels displayInput_noWidth">Age</td>
   </tr>                      
   </HeaderTemplate>

   <ItemTemplate>
   <tr>
   <td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryLastName") %></td> 
   <td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryFirstName") %></td>
   <td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryMiddleName") %></td>
   <td><%#DataBinder.Eval(Container.DataItem, "BeneficiaryAka") %></td>
   <td><%#DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString()).ToString("MM/dd/yyyy")%></td>
   <td> <%(DateTime.Today.Subtract(DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString())).Days / 365).ToString()%></td> 


   </tr>                            
   </ItemTemplate>
   <FooterTemplate>
   </table>
   </FooterTemplate>
   </asp:Repeater>

I figured out how to just format into a date string I like, but what I'm looking for is to subtract today's date from the birthday to get the age of each Beneficiary in the repeater.

I tried to do the following calculation:

<td> <%(DateTime.Today.Subtract(DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString())).Days / 365).ToString()%></td> 

but it wouldn't recognize the DataBinder.Eval container and asked me to add another using statement: System.ComponentModel.Container. Can someone tell me what I'm missing? Thanks in advance.

4

2 回答 2

1

You are missing a # in your code:

Write

<td> <%#(DateTime.Today.Subtract(...

instead of

<td> <%(DateTime.Today.Subtract(...

You can only use the Container variable in a databinding context, which you don't have if you use the normal nugget syntax instead of the databinding syntax.

Also you can shorten your code a lot if you just write Eval("yxz") instead of DataBinder.Eval(Container.DataItem,"xyz"). The first is a shorthand for the second, where the dataitem is automatically assumed.

于 2013-03-08T17:51:46.127 回答
0

It looks like you are missing the #

<td> <%#(DateTime.Today.Subtract(DateTime.Parse(DataBinder.Eval(Container.DataItem, "BeneficiaryBirthday").ToString())).Days / 365).ToString()%></td> 

As an alternative though, consider creating a method to wrap your collection and using the foreach:

<table>      
    <tr>
        <td class="labels displayInput_noWidth">Last Name</td>
        <td class="labels displayInput_noWidth">First Name</td>
        <td class="labels displayInput_noWidth">Middle Name</td>
        <td class="labels displayInput_noWidth">Aka</td>
        <td class="labels displayInput_75w">Birthday</td>
        <td class="labels displayInput_noWidth">Age</td>
   </tr>
   <% foreach (var beneficiary in GetBenificiaries())
   {%> 
   <tr>
       <td><%= beneficiary.BeneficiaryLastName %></td> 
       <td><%= beneficiary.BeneficiaryFirstName %></td>
       <td><%= beneficiary.BeneficiaryMiddleName %></td>
       <td><%= beneficiary.BeneficiaryAka %></td>
       <td><%= beneficiary.BeneficiaryBirthday.ToString("MM/dd/yyyy")%></td>
       <td> <% DateTime.Now.Subtract(beneficiary.BeneficiaryBirthday).Days / 365%></td> 
   </tr> 
   <%}%>

This way it is a lot more readable, you get strong typed intellisense, it matches more closely with MVC if you ever migrate, and if you use tools like resharper, they will be able to refactor these changes better

于 2013-03-08T18:06:03.270 回答