jQuery webforms value created by jQuery does

Charles Baldo
Charles Baldo used Ask the Experts™
on
I have these webform controls

<asp:TextBox ID="HoursWorkedMonday" CssClass="data-input rt" ClientIDMode="Static" runat="server" TabIndex="2" />
....
....
....
<asp:TextBox ID="HoursWorkedSaturday" CssClass="data-input rt" ClientIDMode="Static" runat="server" TabIndex="2" />
...
...
 <asp:TextBox ID="TotalRegular" CssClass="data-input" ClientIDMode="Static" runat="server" Enabled="False" /></div>

The hours are for TotalRegular are accumulated with jquery

       

            $(".rt").each(function () {

                $(this).focusout(function () {
                   [b] calculateRegularHoursSum();[/b]
                });
            });

  function [b]calculateRegularHoursSum()[/b] {
            var sum = 0;
            $(".rt").each(function () {

                if (!isNaN(this.value) && this.value.length != 0) {
                    sum += parseFloat(this.value);
                }
            });
            $("#TotalRegular").val(sum)
        }

Open in new window


If I populate the controls from code behind in c#  when I run this code in jQuery



                    var reghours = $("#TotalRegular").val();
                        if (!reghours.trim()) reghours = 0;
                        $("[id$='regularhours']").html(reghours).text();



        <div id="ConfirmPanel" style="display:none">
            <div class="dialog-label">Regular Hours</div>
            <asp:Label ID="regularhours" CssClass="hours-label" runat="server" /><br />
            <div class="dialog-label">Overtime Hours</div>
            <asp:Label ID="overtimehours" CssClass="hours-label" runat="server" /><br /><br />
        </div>

Open in new window



the dialog displays nothing.  Only if I go in one of those fields and do a focusout() does the control have values
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Manager / Solutions Architect
Commented:
That's a bit of a mess of code formatting there, but as far as I understood, you're only calling calculateRegularHoursSum onfocus out, so it makes sense that the calculation only gets updated if you leave one of the boxes.

Probably what you want is to add this to your code:
$(document).ready(function(){
	calculateRegularHoursSum();
});

Open in new window

This triggers the calcullation when the page loads.
Charles BaldoSoftware Developer

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial