使用Javascript的Asp.NET2.0中存在不明确错误

使用Javascript的Asp.NET2.0中存在不明确错误,javascript,asp.net,Javascript,Asp.net,我在使用下面的javascript时遇到了非常奇怪的问题。编译器在文本框代码的第39行抛出错误,但它是非常正确的。不知道为什么会发生这种情况 <script type="text/javascript"> function WaterMark(txtName, event) { var defaultText = "Enter Username Here"; // Condition to check textbox length and e

我在使用下面的javascript时遇到了非常奇怪的问题。编译器在文本框代码的第39行抛出错误,但它是非常正确的。不知道为什么会发生这种情况

 <script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox length and event type
        if (txtName.value.length == 0 & event.type == "Load") {
        //if condition true then setting text color and default text in textbox
        txtName.style.color = "Gray";
        txtName.value = defaultText;
    }
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "TextChanged") {
        txtName.style.color = "black";
        txtName.value = "";
    }
    }

函数水印(txtName、事件){
var defaultText=“在此处输入用户名”;
//用于检查文本框长度和事件类型的条件
if(txtName.value.length==0和event.type==“加载”){
//如果条件为真,则在文本框中设置文本颜色和默认文本
txtName.style.color=“灰色”;
txtName.value=defaultText;
}
//用于检查文本框值和事件类型的条件
if(txtName.value==defaultText&event.type==“TextChanged”){
txtName.style.color=“黑色”;
txtName.value=“”;
}
}


是服务器端控件。它有几个服务器端。
当aspx标记属性名称与此事件的名称匹配时,asp尝试查找此事件的函数。
在您的情况下,我认为您需要添加客户端事件处理程序,但asp认为您指的是服务器端

首先:输入文本更改的客户端事件-是

second:至于
load
事件-您不能为type=text的输入添加此项,因此您可以使用which check needed text框,或查看此项

所以在finish中,您的标记将如下

<asp:TextBox ID="Tbox" runat="server" Width="300"  onchange ="WaterMark(this,event);" ></asp:TextBox>

我认为您混淆了textbox服务器端事件和js客户端事件onload@Grundy,我想您必须查看错误显示,它显示我在OnLoad事件和Textchange事件的textbox方法调用上有错误,其中还建议我应用}在方法调用上正确吗?我看到了,我重复一遍:您混淆了textbox服务器端事件和客户端事件。@Grundy,我不明白,你能详细解释一下吗。如果我在js中从“加载”改为“加载”,那么它还能工作吗?。js中的TextChange事件是否正确。asp.net2.0服务器端控件(如textbox)事件onclik和onchange不可用…是的,如果aspx标记中的属性与服务器端事件和属性不匹配,则它只需呈现为htmlattribute@user2741987,所以你想在服务器端还是客户端执行此操作?我认为客户端比你想的要好得多?@user2741987,所以,只需使用我答案中的代码:-)此外,如果你不使用服务器端这个文本框中的值,那么最好使用带有
type=“text”
<asp:TextBox ID="Tbox" runat="server" Width="300"  onchange ="WaterMark(this,event);" ></asp:TextBox>
<script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox length and event type
        if (txtName.value.length == 0 & event.type == "Load") {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "change") {
            txtName.style.color = "black";
            txtName.value = "";
        }
    }

    window.onload = function () {
        var defaultText = "Enter Username Here";
        var txtName = document.getElementById('<%: Tbox.ClientID %>');
        // Condition to check textbox length and event type
        if (txtName.value.length == 0) {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
    }
</script>
<asp:TextBox ID="Tbox" runat="server" Width="300"  onclick ="WaterMark(this,event);" ></asp:TextBox>

<script type="text/javascript">
    function WaterMark(txtName, event) {
        var defaultText = "Enter Username Here";
        // Condition to check textbox value and event type
        if (txtName.value == defaultText & event.type == "click") {
            txtName.style.color = "black";
            txtName.value = "";
        }
    }

    window.onload = function () {
        var defaultText = "Enter Username Here";
        var txtName = document.getElementById('<%: Tbox.ClientID %>');
        // Condition to check textbox length and event type
        if (txtName.value.length == 0) {
            //if condition true then setting text color and default text in textbox
            txtName.style.color = "Gray";
            txtName.value = defaultText;
        }
    }
</script>