无法设置属性' innerText'未定义或空引用

时间:2014-06-02 19:56:01

标签: javascript asp.net properties innertext

我写了这段代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
// <![CDATA[

    function btnClient_onclick() {
        document.getElementById("lblClient").innerText = "Changed";
        document.getElementById("lblServer").innerText = "Server";

    }

// ]]>
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Welcome to ASP.NET!
</h2>
<p>
    <asp:Button ID="btnServer" runat="server" onclick="btnServer_Click" 
        Text="Server" />
    <asp:Label ID="lblServer" runat="server" Text="Server"></asp:Label>
</p>
<p>
    <input id="btnClient" type="button" value="Client" onclick="return btnClient_onclick()" />
    <asp:Label ID="lblClient" runat="server" Text="Client"></asp:Label>
</p>
</asp:Content>

当我运行它时,得到了这个错误:无法设置属性&#39; innerText&#39;未定义或空引用

2 个答案:

答案 0 :(得分:0)

设置

  

ClientIDMode为静态

两个标签上的

并查看是否有效

答案 1 :(得分:0)

试试这个

document.getElementById("lblClient").setAttribute('text', "Changed");