Problem with capturing enter key in TextBox within UserControl

  • Thread starter marcus.mordenfeld
  • Start date
M

marcus.mordenfeld

Hi,

I have a problem that is probably quite logic when you know the basics
behind it, but it has troubled my mind the last three hours;

I'm trying to capture the enter key in a TextBox within a UserControl,
no matter where the UserControl is placed.

My setup is as follows:

I have two Custom UserControls; Login and UserSearch.
I have a MasterPage, site.master, containing the Login UserControl and
a ContentPlaceHolder named Body. In my site.master source (html) I have
the following javascript:

<script type="javascript">
function submit_on_enter(btnId)
{
var btn = document.getElementById(btnId);
if (window.event.keyCode == 13)
{
window.event.returnValue=false;
window.event.cancel = true;
btn.click();
}
}
</script>

In the Login.ascx, I have implemented the following code in Page_Init:

string buttonId = this.Parent.NamingContainer.ClientID + "_" + this.ID
+ "_" + lbtnLogin.ID;
tbPassword.Attributes.Add("onKeyDown", "return submit_on_enter('" +
buttonId + "');");

binding the javascript attribute to the password TextBox. I first tried
to pass lbtnLogin.ID to the javascript, but I saw in the page source
(in the browser) that lbtnLogin got the ID "ctl00_ucLogin_lbtnLogin",
which resulted in the document.getElementById('lbtnLogin') failing in
finding the button.

My solution worked fine... until I tried to use the same approach on my
other UserControl, SearchUser, that is used in the content page
Users.aspx (pointing at the site.master ContentPlaceHolder Body).
The search button (same as the lbtnLogin, but in the search
UserControl) then gets the id "ctl00_Body_ucSearch_lbtnSearch".

I can clearly see the pattern of ASP.NET building the new ID, but I
can't see the easy solution of my problem?!

I want to be able to refer to my LinkButton wherever I place my
UserControl. Isn't that the purpose of a UserControl, to be able to
move it around?!?

I would appreciate any help!

Regards,
Marcus Mordenfeld
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top