开发者

clear the labels text value

开发者 https://www.devze.com 2023-01-18 18:47 出处:网络
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"Script2.aspx.cs\" Inherits=\"Javascript.Script2\" %>
<%@ 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)
        {

        }
    }
}

here iam not able to clear the text value of Label .

once i clcik the button.here i am trigerring the function to clear开发者_如何学编程 the labels values.

but the text is not getting cleared.

any idea how to solve the issue.

thanks.


The ID of an ASP.NET control is not the same as the id in the DOM. The HTML element's ID is generated by ASP.NET at runtime.

You can specify the DOM id by specifying the ClientID attribute:

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


It seems that labels are not suitable for this purpose. Labels changed client-side do not keep their values on PostBacks. use a textbox instead and make it look like a label by setting borderwidth=0

 <asp:TextBox ID="lbl_error" BorderColor="White" BorderStyle="None"   BorderWidth="0" runat="server" Width="99%"></asp:TextBox>

then in javascript write this function

function resetlabel() {

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

精彩评论

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