Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

Removing ASP.NET Label text from JavaScript

I am removing some character from asp.net Label Text with JS, Its removing the text, but on code behind file its still having the text. Below is my code

function GetClientID(id, context) {
            var el = $("#" + id, context);
            if (el.length < 1)
                el = $("[id$=_" + id + "]", context);
            return el;
        }

 var emaillbl = GetClientID("lblEmail").attr("id");//lblEmail is ID of asp.net Label control

$("#" + emaillbl).html($("#" + emaillbl).html("-",''));

and on my code Behind

if(lblEmail.Text != "")//This condition always getting false
{
}
else
{
}

I have also tried with these but no luck

 $("#" + emaillbl).text($("#" + emaillbl).html("-",''));

 $("#" + emaillbl).val($("#" + emaillbl).html("-",''));

this is my Label

<asp:Label ID="lblEmail" runat="server"></asp:Label>

Upvotes: 0

Views: 935

Answers (3)

Sudhanshu Shekhar
Sudhanshu Shekhar

Reputation: 609

It is happening because the previous value is stored in the viewstate. Add the below mentioned attribute value pair to the control to disable view state for the control.

EnableViewState="false"

Upvotes: 0

Nikolaj Zander
Nikolaj Zander

Reputation: 1270

Use a <asp:HiddenField> and also write the changed value in that field!

In code behind change your label with the value from your hiddenfield!

to write the value using jquery use:

<asp:HiddenField runat="server" ID="myHiddenField"/>

$('#<%= myHiddenField.ClientID %>').val(myNewValue);

Upvotes: 1

Aditya Singh
Aditya Singh

Reputation: 9612

Try this out:-

jQuery(function($){
  $("#<%=lblEmail.ClientID%>").remove();
});

And if you want to reuse your code then,

function GetClientID(id, context) {
            var el = $("#" + id, context);
            if (el.length < 1)
                el = $("[id$=_" + id + "]", context);
            return el;
        }

var emaillbl = GetClientID("lblEmail").attr("id");

$("#" + emaillbl).remove();

Upvotes: 0

Related Questions