Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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
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
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 10-Oct-12 2:22am
Edited 10-Oct-12 2:47am
v3
Comments
Milind Thakkar at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can try hiding the control with following code.
 
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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this
 

var obj = document.getElementById("<%=label1.ClientID %>");
obj.style.display = 'none';
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
if(!IsPostBack)
{
//call function to populate the values
}
On buttons click event clear the label's value
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:
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

ClientInstanceName.SetVisible(false);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try this:-
 

function clearLabelValue(){
     var labelObj = document.getElementById("<%= myLabel.ClientID %>");
     labelObj.value = "";
  }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

document.getElementById('Label1').style.display = 'none';
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100