Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
How can I clear label text using javascript in asp.net..?

I want to clear the text of my Label on Button(asp:Button) Click, by calling a java script function.

I tried
C#
document.getElementByID("label1").innerHTML='';

It cleared,but suddenly it getting the value back,because of postback..
i can't use these following code in page load
C#
if (!IsPostBack)
{
   label1.Text = "";
}

any other option for this. Otherwise tell me how can i hide the label on Button(asp:Button) Click, by calling a java script function.
I used these code to hide
var obj = document.getElementById("label1");
obj.style.display = "none";
obj.style.visibility = "hidden";
Again it's not working in my case because of postback.

Pls help me...
Thanks ,
Vineetha.K.R
Posted
Updated 10-Oct-12 1:47am
v3
Comments
MT_ 10-Oct-12 7:55am    
I see that you put in the code I suggested, why can't you hide it on page_Load or pre_init?

You can try hiding the control with following code.

JavaScript
var obj = document.getElementById("label1");
obj.style.display = "none";
obj.style.visibility = "hidden";


Also, you can hide it On Page_Load

i.e. label1.Visible=false;

Isnt it?

Hope that helps. If it doesn, mark it as answer/upvote.
-Milind
 
Share this answer
 
v3
Try this


var obj = document.getElementById("<%=label1.ClientID %>");
obj.style.display = 'none';
 
Share this answer
 
Yes, the reason your label is persisting its value on postback is because you are calling some method that populates the label's value on PageLoad event. Wrap this code inside
C#
if(!IsPostBack)
{
//call function to populate the values
}
On buttons click event clear the label's value
C#
Protected Void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
}
and if you want to populate the values again without the label persisting its value use:
C#
Protected Void Button1_Click(object sender, EventArgs e)
{
//call function to populate the values
Label1.Text = "";
}Hope it helps. If it doesn't work post your complete code.
 
Share this answer
 
ClientInstanceName.SetVisible(false);
 
Share this answer
 
try this:-


C#
function clearLabelValue(){
     var labelObj = document.getElementById("<%= myLabel.ClientID %>");
     labelObj.value = "";
  }
 
Share this answer
 
document.getElementById('Label1').style.display = 'none';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900