views:

221

answers:

3

Why does this statement work with OnKeyPress event of Javascript in C#.

 txtPassword.Attributes.Add("OnKeyUp", "CheckPasswordStrength(\"" 
                + txtPassword.ClientID.ToString() + "\",\""+  lblMessage.ClientID.ToString() +"\")");

this code is working correctly, my problem is that i want to run keypress event not keyup event

+1  A: 

You should check the rendered output by viewing source of the page but I think your problem is about double quotes. Replace them with single quotes.

txtPassword.Attributes.Add("OnKeyUp", "CheckPasswordStrength('" 
            + txtPassword.ClientID.ToString() + "','"+  lblMessage.ClientID.ToString() 
+"')");

this will probably rendered as :

<input onkeyup="CheckPasswordStrength('clientIdhere', 'clientIdhere');" />
Canavar
I believe javascript works with double or single quotes.
dcp
yes tehy work but the surrounding double quotes are the problem :)
Canavar
Yep, good catch :).
dcp
this code is working correctly, my problem is that i want to run keypress event not keyup event
Shantanu Gupta
@Shantanu Gupta : Did you try to attach your function to onkeypress event ?
Canavar
yes, I tried but it is not working, I even tried onChange and ontextChanged. But only this onKeyUp is working
Shantanu Gupta
between onkeyup, onkeydown and onkeypress events there is a little difference. What do you want to do exactly ?
Canavar
I want to work on onkeypress event. I know the difference among all. Although onkeyUp event can work for me but i just wanted to check onKeyPress event.
Shantanu Gupta
A: 

Change "onkeyup" to "onkeypress?" Maybe I'm not understanding.

D. Patrick
I tried to do it, but when i changes it from onkeyup to onkeypress it doesn't works
Shantanu Gupta
+1  A: 

Use on onKeyDown for Windows/MSIE.

drlouie - louierd
I want to use OnKeyPress
Shantanu Gupta
Good luck on that bud, it's not going to work by what you want it to work as. The markup for my web apps are always dynamically generated based on the principles of the browser at play meaning, MSIE get onKeyDown and all others get onKeyPress. This is the only quick and easy way around your issue, really.
drlouie - louierd
"The markup for my web apps are always dynamically generated based on the principles of the browser at play meaning". How do u gets that ?
Shantanu Gupta
What's your back-end language look like? Meaning is it PHP, Perl, ASP, Java?
drlouie - louierd
If none of the above we can use javascript to write out your form dynamically as well based on the browser being utilized.
drlouie - louierd
I m using asp.net with C#, Browser IE8/Mozilla
Shantanu Gupta