Capturing Enter KeyDown in a TextBox

G

George Durzi

I have a text box and a button, and I want the enter key to run the click
event of the button. The textbox and button are inside a user control. I
tried all sorts of stuff with the __EVENTTARGET hidden field with no luck.

Here's what I'm doing

function KeyDownHandler(btn)
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
btn.click();
}
}

<asp:TextBox ID="txtNewsFeedName" Runat="server" MaxLength="100"
Columns="35" onKeyDown="KeyDownHandler('btnCreateNewsFeed')"></asp:TextBox>
<asp:button ID="btnCreateNewsFeed" Runat="server" Text="Add"
CssClass="submitbutton" CausesValidation="False"></asp:button>



When I hit Enter when typing inside the TextBox, the KeyDownHandler function
gets executed, but I get an error on the btn.click(); line.

The error is: object doesn't support this property or method.

Verified that btn is coming in as btnCreateNewsFeed. Dunno if you need to do
casting in JavaScript, but how is this function supposed to know that this
is a button...

How can fire the click event of that button?
 
H

hb

Think about the value you pass to the function. You are passing the ID of
the btn, so you can't just call btn.click(); - that's the same as saying
'btnCreateNewsFeed'.click() and that will not work.

Instead, replace btn.click(); with document.getElementById(btn).click();
 
G

George Durzi

As I mentioned, by textbox and button are in a user control,

So I actually have to do KeyDownHandler('usercontrolname:btnCreateNewsFeed')
since that's how the element is being rendered.

And of course, as you suggested, with the
document.getElementById(btn).click();

It now works

Thank you
 
H

hb

George Durzi said:
As I mentioned, by textbox and button are in a user control,

So I actually have to do KeyDownHandler('usercontrolname:btnCreateNewsFeed')
since that's how the element is being rendered.

And of course, as you suggested, with the
document.getElementById(btn).click();

It now works

Thank you

Glad you figured out the ID - I thought about that after I posted my reply
but hoped you would realize the solution.

I usually make sure the Button's Client ID is always correct by assigning
the onKeyDown event in code-behind, like this:

textBox.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID +
"')");

Why? Because 'usercontrolname:btnCreateNewsFeed' doesn't apply if you for
any reason add the control dynamically, because then it is something like
'_ctl2:btnCreateNewsFeed'.
 
G

George Durzi

That's a great solution, thank you

hb said:
Glad you figured out the ID - I thought about that after I posted my reply
but hoped you would realize the solution.

I usually make sure the Button's Client ID is always correct by assigning
the onKeyDown event in code-behind, like this:

textBox.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID +
"')");

Why? Because 'usercontrolname:btnCreateNewsFeed' doesn't apply if you for
any reason add the control dynamically, because then it is something like
'_ctl2:btnCreateNewsFeed'.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top