TextChanged vs. [Button] Click Event

G

Guest

I'm new to ASP.net programming so excuse my ignorance if the following
question seems overly simplistic.

I've created a simple Login form with 3 primary WebControls:
- A TextBox for the Username
- A TextBox for the Password
- A Button labelled "Submit"

If the user enters a correct username/password combination then he's taken
to another page. But if either or both are incorrect then a simple message
is displayed. Everything works perfectly fine if he clicks the Submit
button. But I activated the same code for the TextChanged event of the
Password TextBox so that the user could just press 'Enter' after entering the
password.

For reasons I can't explain, though I think it has something to do with
PostBack, the message is not displayed when TextChanged event is fired. Yet
it does work correctly when the button's Click event is fired. Why is this?
 
G

Guest

It should work in either case. Double check the control flow of your code to
ensure the event with your logic is called.

The text change event probably is not the most efficient however, since you
would have to test both in the username and password text changed events -
either one might have changed on the post back.

The button click might work better and it can receive and event when the
enter key is pressed.

- Steve
 
G

Guest

Steve,

Thanks for responding. Can I ask you to elaborate on the last thing you
said: "The button click might work better and it can receive an event when the
enter key is pressed."

How do I get the Button Click event to "receive" the event of teh Enter key
being pressed? If I could do that, I think it would simplify things greatly!
 
G

Guest

Hi Steve,

After I read your response, I did some more research on my own. I
discovered these two articles:

http://aspnet.4guysfromrolla.com/articles/060805-1.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

Both seemed to be describing my problem exactly, though I was puzzled
because I already did have 2 textboxes. Regardless, in the 2nd one I saw
this code:

TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if
((event.which == 13) || (event.keyCode == 13))
{document.getElementById('"+Button1.UniqueID+"').click();return false;}} else
{return true}; ");


So I added it and voila, all works great now!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top