Shahid Iqbal
Shahid Iqbal

Reputation: 2135

Changing asp.net Label text in JQuery

I have a Label and i want to change its value from JQuery. The new value is assigned to label and changed to default value automatically. Means the new value is lost with post back on button. When alert message is displayed, new value is assigned to label, but on clicking on alert msg, default value is again restored. Below is my code.

       $(document).ready(function () {
            $("#btnSave").click(function () {
                var value = $("#txtName").val().trim();
                if (value == '') {
                     $('#<%=HFIsValid.ClientID %>').html("false");
              //    $('#<%=HFIsValid.ClientID %>').text("false");
              //    $('#HFIsValid%>').text("false");
              //  All the above 3 statements works simailarly
                    alert('Name Requried');
              // Here when alert comes new value is displayed, but after clicking ok on
              // alert message, default value is restored again.
                   return;
                }
            });
        });

Thanks in advance...

Upvotes: 0

Views: 518

Answers (1)

Thomas
Thomas

Reputation: 1563

When you're doing a postback, you're assigning the default values once again in your page_load() etc.

Try to, instead of just

return;

add

return false;

This will prevent the normal button onClick() behaviour which courses a postback

Upvotes: 1

Related Questions