开发者

What is the problem with this onload event?

开发者 https://www.devze.com 2023-01-04 17:01 出处:网络
I have this password field input class=\"txtfld\" type=\"password\" id=\"TextBoxLoginPassword\"runat=\"server\"

I have this password field

input class="txtfld" type="password" id="TextBoxLoginPassword"  runat="server" 
                value="password"     
                onfocus="javascript:if(this.value == 'password') this.value='';" 
                onblur="javascript:if(this.value == '') this.value='password';" 
                onload="javascript:if(this.value == '') this.value='password';"
                validationgroup="Login" />

Problem is that this appears as empty. Even I have set a default value. To solve this problem I have added an onload event and I get following error

  1. Empty Character Literal
  2. Too many Charact开发者_开发百科ers in character literals Why does this appear empty in first place and why do I get these errors? What I need is to show a default value in password field.


The problem is in the combination of the text in the OnLoad and the runat=server.

Because of that runat attribute, asp.net thinks the OnLoad specifies a server-side function. And then '' and 'password' seem (at least to C#) to specify character literals, of which the first has too few actual characters ("Empty Character Literal") and the second too many.

So try to remove the "runat=server" (and read the password directly from the QueryString)


It seems to me that you want to provide a "watermark", not the real password. Have you looked at the Watermark extender of the Ajax control toolkit?


@Hans Kesting answer is correct, but if you don't want to use extender, you can use the following code.

<input class="txtfld" type="text" id="TextBoxLoginPassword"   
                value="enter password"     
                onfocus="javascript:if(this.type != 'password') this.value='';this.type='password'" 
                 />


Here is a really sweet bit of JQuery / CSS that you could use here and elsewhere in your site to solve your problem:

http://attardi.org/labels/#demo

0

精彩评论

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