从出生日期算起年龄

时间:2018-08-04 12:46:55

标签: ajaxcontroltoolkit

我正在使用以下代码从出生日期算起年龄 该代码可在ASP.NET Web窗体页面中使用。 但是它不能在MasterPage上工作。 ScriptManager标签包含在母版页中

                  <div class="form-group">

                    <asp:Label runat="server" AssociatedControlID="dob" CssClass="col-md-2 control-label">Date of Birth : </asp:Label>
                    <div class="col-md-10">
                         <asp:TextBox ID="dob" runat="server" CssClass="form-control" > 
                             </asp:TextBox>
                       <cc1:CalendarExtender runat="server" TargetControlID="dob" Format="yyyy-MM-dd" OnClientDateSelectionChanged="SelectDate" ID="CalendarExtender1"></cc1:CalendarExtender>
                        <script type="text/javascript">

                          function SelectDate(e) {

                           var PresentDay = new Date();

                            var dateOfBirth = e.get_selectedDate();

                             var months = (PresentDay.getMonth() - dateOfBirth.getMonth() + (12 * (PresentDay.getFullYear() - dateOfBirth.getFullYear())));

                         alert("Your age : " + Math.floor(months / 12) + " Years");

                         document.getElementById("txtage").value = Math.floor(months / 12);

                             }

</script>
                         <asp:RequiredFieldValidator runat="server" ControlToValidate="dob" CssClass="text-danger" ErrorMessage="Date of Birth is required." />
                    </div>
                </div>


                 <div class="form-group">
                    <asp:Label runat="server"  CssClass="col-md-2 control-label" Text="Age : "> </asp:Label>
                    <div class="col-md-10">
                         <asp:TextBox ID="txtage"  runat="server" CssClass="form-control" />
                      </div>
                </div>

更新::

我通过使用以下代码解决了

                       出生日期 :                                                                                                                                                   

                          function SelectDate(e) {

                           var PresentDay = new Date();

                            var dateOfBirth = e.get_selectedDate();

                             var months = (PresentDay.getMonth() - dateOfBirth.getMonth() + (12 * (PresentDay.getFullYear() - dateOfBirth.getFullYear())));

                         //alert("Your age : " + Math.floor(months / 12) + " Years");
                         document.getElementById('<%=age.ClientID %>').value = Math.floor(months / 12);

}

</script>
                         <asp:RequiredFieldValidator runat="server" ControlToValidate="dob" CssClass="text-danger" ErrorMessage="Date of Birth is required." />
                    </div>
                      <asp:Label AssociatedControlID="age" runat="server"  CssClass="col-md-3 control-label" Text="Age : "> </asp:Label>
                    <div class="col-md-3">
                         <asp:TextBox ID="age"  runat="server" CssClass="col-md-12 form-control" />
                      </div>
                </div>

0 个答案:

没有答案