Search code examples
asp.netlabelhiddenfield

Storing Label value in Hidden Field


I have a read only label and it gets the value once the user enters the Height and Weight values. Everything is working great but once I tried storing the label value to database its always empty and I read that after postback labels lose their values. So I read about hidden field but I could not find the right syntax of how to add label as the hidden field value, I used hidden fields before with EVAL but that only works while binding data.

help will be appreciated.

  <asp:Label ID="lblBMI" runat="server" ClientIDMode="Static" ReadOnly="true"></asp:Label>



                                <script type="text/javascript">
                                    $(document).ready(function () {

                                        CalculateBodyMassIndex();

                                        $('#txtHeight').change(function () {
                                            CalculateBodyMassIndex();
                                        });

                                        $('#txtWeight').change(function () {
                                            CalculateBodyMassIndex();
                                        });

                                        function CalculateBodyMassIndex() {
                                            var floatNumber= /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
                                            var height = $('#txtHeight').val();
                                            var weight = $('#txtWeight').val();

                                            if (height != "" && weight != "") {
                                                if (floatNumber.test(height) && floatNumber.test(weight)) {
                                                    var bmi = weight / (height * height)
                                                    $('#lblBMI').text(bmi.toFixed(2));

                                                } else {

                                                    alert('Only numbers are allowed');
                                                }
                                            } else {
                                                $('#lblBMI').text("");
                                            }
                                        }
                                    });
                                </script>

Solution

  • The changed made to a Label value client side will not be posted to the server and therefore will always be empty. You need to store it in a input field. The snippet below will set the BMI value to the hidden field also and that value can be retrieved in code behind.

    <asp:Label ID="lblBMI" runat="server"></asp:Label>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    
    <script type="text/javascript">
        var bmi = 5.3;
        $('#<%= lblBMI.ClientID %>').text(bmi.toFixed(2));
        $('#<%= HiddenField1.ClientID %>').val(bmi.toFixed(2));
    </script>