清除标签文本值 函数ClearValue(Text1、Text2){ var txtClear1=document.getElementById(Text1); var txtClear2=document.getElementById(Text2); 如果(txtClear1!=null | | txtClear2!=null) { txtClear1.outerText=“”; txtClear1.value=“”; txtClear1.innerText=“”; txtClear1.innerHTML=“”; txtClear1.outerHTML=“” txtClear2.value=“”; txtClear2.innerText=“”; txtClear2.innerHTML=“”; txtClear2.outerHTML=“” 返回false; } } 名称空间Javascript { 公共部分类Script2:System.Web.UI.Page { 受保护的无效页面加载(对象发送方、事件参数e) { 添加(“onclick”、“ClearValue(“+lblError1.ClientID+”、“+lblError2.ClientID+”)); } 受保护的void btnClose_单击(对象发送者,事件参数e) { } } }

清除标签文本值 函数ClearValue(Text1、Text2){ var txtClear1=document.getElementById(Text1); var txtClear2=document.getElementById(Text2); 如果(txtClear1!=null | | txtClear2!=null) { txtClear1.outerText=“”; txtClear1.value=“”; txtClear1.innerText=“”; txtClear1.innerHTML=“”; txtClear1.outerHTML=“” txtClear2.value=“”; txtClear2.innerText=“”; txtClear2.innerHTML=“”; txtClear2.outerHTML=“” 返回false; } } 名称空间Javascript { 公共部分类Script2:System.Web.UI.Page { 受保护的无效页面加载(对象发送方、事件参数e) { 添加(“onclick”、“ClearValue(“+lblError1.ClientID+”、“+lblError2.ClientID+”)); } 受保护的void btnClose_单击(对象发送者,事件参数e) { } } },javascript,Javascript,这里我无法清除标签的文本值 一旦我点击按钮,这里我触发函数来清除标签值 但是文本并没有被清除 你知道怎么解决这个问题吗 谢谢。ASP.NET控件的ID与DOM中的ID不同。HTML元素的ID由ASP.NET在运行时生成 您可以通过指定ClientID属性来指定DOMid: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Script2.aspx.cs" Inherits="Javascript.Script2" %>

这里我无法清除标签的文本值

一旦我点击按钮,这里我触发函数来清除标签值

但是文本并没有被清除

你知道怎么解决这个问题吗


谢谢。

ASP.NET控件的ID与DOM中的ID不同。HTML元素的ID由ASP.NET在运行时生成

您可以通过指定ClientID属性来指定DOMid

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Script2.aspx.cs" Inherits="Javascript.Script2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="javascript" type="text/javascript">
    function ClearValue(Text1, Text2) {
        var txtClear1 = document.getElementById(Text1);
        var txtClear2 = document.getElementById(Text2);
        if (txtClear1 != null || txtClear2 != null)
         {
            txtClear1.outerText = "";
            txtClear1.value = "";
            txtClear1.innerText = "";
            txtClear1.innerHTML = "";
            txtClear1.outerHTML = ""

            txtClear2.value = "";
            txtClear2.innerText = "";
            txtClear2.innerHTML = "";
            txtClear2.outerHTML = ""

            return false;
        }
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblError1" runat="server" Text="Label1"></asp:Label>
        <asp:Label ID="lblError2" runat="server" Text="Label2"></asp:Label>
        <asp:Button ID="btnClose" runat="server" Text="Button" 
            onclick="btnClose_Click"   />  
    </div>
    </form>
</body>
</html>



namespace Javascript
{
    public partial class Script2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            btnClose.Attributes.Add("onclick", "ClearValue('" + lblError1.ClientID + "','" + lblError2.ClientID + "')");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

        }
    }
}

标签似乎不适合此用途。客户端更改的标签不会在回发中保留其值。改为使用文本框,并通过设置borderwidth=0使其看起来像标签

<asp:Label ID="lblError1" ClientID="lblError1" runat="server" Text="Label1"></asp:Label>

然后用javascript编写这个函数

 <asp:TextBox ID="lbl_error" BorderColor="White" BorderStyle="None"   BorderWidth="0" runat="server" Width="99%"></asp:TextBox>
函数resetlabel(){
document.getElementById(“”.value=“”;
}

根据经验,当语言a(.NET)动态输出语言B(JavaScript)时,调试的第一步应该是检查生成的代码。然后你的问题变成了“为什么.NET输出这个而不是那个?”或者“为什么这个JavaScript失败?”——这更容易回答(并且让更多的人回答,因为它不需要两种语言的知识)。请特别注意生成的控件ID和名称。例如,页面加载可能在页面生命周期中发生得太快,无法安全地访问ClientID属性。您可以尝试在预渲染阶段添加onclick属性。
function resetlabel() {

            document.getElementById("<%=lbl_error.ClientID%>").value = "";
        }