views:

66

answers:

3

Hi,

I have this simple code:

<html>
<body>

<form name="f1">
<asp:Label name="lbl"  runat="server" Text="Label" onclick="lblClick()"></asp:Label>
</form>

<script type="text/javascript">

function lblClick(){
document.f1.lbl.text="new text";}
</script>

</body>
</html>

It doesn't work, it gives me: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object ... , I can't even change the label text ....why?!!!

+2  A: 
<asp:Label id="lbl" name="lbl"  runat="server" Text="Label" onclick="lblClick(this)"></asp:Label>

function lblClick(elem)
{
    elem.innerText = "new text"; //IE
    //elem.textContent = "new text"; //FF
}
rahul
what is elem ...?
jjj
It is a reference to the label which invoked the function.
rahul
+1  A: 

let us remember that asp.net renames your controls for you.

<form name="f1">
<asp:Label id="lbl" runat="server" Text="Label" onclick="lblClick()"></asp:Label>
</form>

<script type="text/javascript">

function lblClick(){
document.getElementById('<%=lbl.ClientId %>').innerHTML="new text";}
</script>

Adamantium's answer will also provide you the functionality you're after.

Stephen Wrighton
A: 

Thanks all, it was stuped problem .. becouse it was work on the old asp.net but it didn't on the new version ... !! .... thank you all

jjj