Capturing Enter KeyDown in a TextBox

  • Thread starter Thread starter George Durzi
  • Start date Start date
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?
 
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();
 
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
 
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'.
 
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'.
 
Back
Top