开发者

Why can't I call a form content?

开发者 https://www.devze.com 2022-12-14 07:40 出处:网络
I have this simple code: <html> <body> <form name=\"f1\"> <asp:Label name=\"lbl\"runat=\"server\" Text=\"Label\" onclick=\"lblClick()\"></asp:Label>

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?!!!


<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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消