hitting enter button while in textbox calls correct button's click event

T

TS

for some reason, it posts to the server, but no click events of any buttons
on form fire. the button is the first one on the form. when the focus is
inside the textbox, it doesnt' work. if i click the form, it works fine.

how do i make it fire the click event of this button? (i have it on a diff.
page and it works fine...can't figure out why it is diff. here)

thanks to all!

(i tried tab order)
 
J

Jeffrey Tan[MSFT]

Hi TS,

Thanks for your post.

Based on my understanding, you want to get the function that: when the user
hits enter key in the textbox, just trigger the server side Button's Click
event.

To achieve this, we have to do some customization at the client side. We
can use javascript to cancel the current enter key press operation, then
programmatically invoke the button.click() method. Also, we can first
associate the client textbox.onkeypress event with the client javascript
code. Below is the demo code:

private void Page_Load(object sender, System.EventArgs e)
{
this.TextBox1.Attributes.Add("onkeypress","button_click()");
}

<script language="javascript">
function button_click()
{
if(window.event.keyCode==13)
{
var Button1=document.getElementById("Button1");
Button1.click();
window.event.cancel=true;
}
}
</script>

This code works well on my side. Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Thanks i will try that, i'm sure it will work.

Can you tell me though why this is occuring so i can understand the root
cause? Like I said, i have a different page that when I hit enter while
cursor in the textbox, it correctly posts to the server and calls the server
side click event handler and works how i want it, and i'm wondering why it
works there, and not on my other page.

thanks a bunch!
 
T

TS

Also, why does it post to the server at all? it would seem that if it posts
to the server, then obvioulsy the submit button was clicked, or was a
different event evoked that didn't have an event handler?


TS said:
Thanks i will try that, i'm sure it will work.

Can you tell me though why this is occuring so i can understand the root
cause? Like I said, i have a different page that when I hit enter while
cursor in the textbox, it correctly posts to the server and calls the
server
side click event handler and works how i want it, and i'm wondering why it
works there, and not on my other page.

thanks a bunch!

"Jeffrey Tan[MSFT]" said:
Hi TS,

Thanks for your post.

Based on my understanding, you want to get the function that: when the
user
hits enter key in the textbox, just trigger the server side Button's
Click
event.

To achieve this, we have to do some customization at the client side. We
can use javascript to cancel the current enter key press operation, then
programmatically invoke the button.click() method. Also, we can first
associate the client textbox.onkeypress event with the client javascript
code. Below is the demo code:

private void Page_Load(object sender, System.EventArgs e)
{
this.TextBox1.Attributes.Add("onkeypress","button_click()");
}

<script language="javascript">
function button_click()
{
if(window.event.keyCode==13)
{
var Button1=document.getElementById("Button1");
Button1.click();
window.event.cancel=true;
}
}
</script>

This code works well on my side. Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
T

TS

More info:

I am setting the focus to the textbox on page load so the user can
immediately type in a number they want to search and then hit enter key to
submit. Again this is the exact same steps used on the other page that
functions correctly.

thanks again

TS said:
Thanks i will try that, i'm sure it will work.

Can you tell me though why this is occuring so i can understand the root
cause? Like I said, i have a different page that when I hit enter while
cursor in the textbox, it correctly posts to the server and calls the
server
side click event handler and works how i want it, and i'm wondering why it
works there, and not on my other page.

thanks a bunch!

"Jeffrey Tan[MSFT]" said:
Hi TS,

Thanks for your post.

Based on my understanding, you want to get the function that: when the
user
hits enter key in the textbox, just trigger the server side Button's
Click
event.

To achieve this, we have to do some customization at the client side. We
can use javascript to cancel the current enter key press operation, then
programmatically invoke the button.click() method. Also, we can first
associate the client textbox.onkeypress event with the client javascript
code. Below is the demo code:

private void Page_Load(object sender, System.EventArgs e)
{
this.TextBox1.Attributes.Add("onkeypress","button_click()");
}

<script language="javascript">
function button_click()
{
if(window.event.keyCode==13)
{
var Button1=document.getElementById("Button1");
Button1.click();
window.event.cancel=true;
}
}
</script>

This code works well on my side. Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi TS,

This behavior is client browser specific, which do not have a definit way.
I think "Steve C. Orr [MVP, MCSD]" has provided you an informative reply
in "how do i determine what event caused postback" post. Please check it
there. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top