How to call javascript function on label OnLoad

0 votes
asked Aug 25, 2010 by breakhead

I want to call javascript function on label onload, reason I am using an enitity Datasource and assignng a value to this label, but I want to hide it if the value is empty.


2 Answers

0 votes
answered Aug 25, 2010 by tarwn

There are 3 different solutions I can think of off the top of my head:

1) Bind an expression to the visible property of the label that returns true if the value is empty and false if it does not (this will hide it server-side)

<asp:Label id="yourLabel" runat="server" Text='<%# Eval("aField") %>' 
Visible='<%# IF(String.IsNullOrEmpty(Eval("aField",True,False))) %>' />

2) If you have jQuery then you can do the following in your script tags:

$(document).ready( function () { $find('label id').hide(); });

3) If you don't have jQuery then you could do some javascript like:

   document.getElementById('<%=yourLabel.ClientID%>').style.display = "none";

Warning: both pieces of javascript were freeform entry, so they may require some tweaking or spell correction to actually work

0 votes
answered Aug 25, 2010 by breakhead
<asp:Label ID="Label2" 
     Font-Bold="true" Visible='<%#   (!String.IsNullOrEmpty(Server.HtmlDecode((string)Eval("Value")))) %>'>
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter