views:

52

answers:

3

I am trying to add the onkeydown attribute to an asp:textbox. For some reason my code can't find the textbox that is inside a loginview.

Am I doing something wrong?

<script type="text/javascript">
window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')");
}

function KeyDownHandler(btn)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        document.getElementById(btn).click();
    }
}
</script>
A: 

Try wiring the event handler parameter this way:

<script type="text/javascript">
    window.onload = function() {
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
    Password.Attributes.Add("onKeyDown", "KeyDownHandler(this)");
}

function KeyDownHandler(domButton)
{
    if (event.keyCode == 13)
    {
        event.returnValue=false;    
        event.cancel = true;
        domButton.click();
    }
}
</script>
Humberto
I tried this and I seem to get the error:Message: 'UserName' is undefinedLine: 18Char: 5Code: 0URI: http://localhost:1972/TiltonSite/
BioXhazard
This too: Message: Object doesn't support this property or methodLine: 28Char: 9Code: 0URI: http://localhost:1972/TiltonSite/
BioXhazard
Your Attributes.Add() calls must be in a `<script runat="server">` tag, and the KeyDownHandler() must be in a client script section (that is, a `<script type="text/javascript">`
Humberto
+1  A: 

Your code is trying to add the event handler attributes in the client script. This needs to happen in a server-side code block. Something like:

<script runat="server"> 
    UserName.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
    Password.Attributes.Add("onKeyDown", "KeyDownHandler('" + btn.ClientID + "')"); 
</script>
<script type="text/javascript">
function KeyDownHandler(btn) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        document.getElementById(btn).click(); 
    } 
} 
</script> 

Alternatively, if you have a code-behind page, put the attribute.Add calls in the PreRender event.

David
I get errors from that. Declaration expected for UserName and Password
BioXhazard
Do you have `<asp:textbox ID="UserName" runat="server">` and `<asp:textbox ID="Password" runat="server">` in your aspx file?
Humberto
This is a masterpage. I have the UserName and Password textboxes inside a loginview. Do I have to specify the loginview perhaps?
BioXhazard
@Andrew Absolutely.
Humberto
How would I do this?loginview#UserName?
BioXhazard
I guess you need to search for the server controls before modifying them, as they may be unavailable if your LoginView renders a template in which they don't exist.
Humberto
How would I do that in Javascript?
BioXhazard
You could also use View Source on the emitted final html, find out the rendered id's of the those user/pass <input> elements and use javascript to wire the events directly in the onload event as per your original example. Here is a brief example to use javascript to wire an event: http://v3.thewatchmakerproject.com/journal/168/javascript-the-dom-addeventlistener-and-attachevent
David
A: 

In your aspx file, add the server script that will bind existing UserName and Password textboxes to a client event handler called KeyDownHandler:

<script runat="server">
   protected void Page_Load(object sender, EventArgs e)
   {
      TextBox userNameControl = FindControl("UserName") as TextBox;
      TextBox passwordControl = FindControl("Password") as TextBox;

      if (userNameControl != null)
         userNameControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 

      if (passwordControl != null)
         passwordControl.Attributes.Add("onKeyDown", "KeyDownHandler(this)"); 
   }
</script>

Then declare the event handler's client script:

<script type="text/javascript">
function KeyDownHandler(domButton) 
{ 
    if (event.keyCode == 13) 
    { 
        event.returnValue=false;     
        event.cancel = true; 
        domButton.click(); 
    } 
} 
</script>
Humberto
It says declaration expected for 'Textbox'
BioXhazard
Updated server side script to include a Page_Load() declaration.
Humberto
This is definitely not going to work. You're using VB and Javascript controls in one script at the same time.
BioXhazard
Actually it is C# (server) and JavaScript (client), definitely *not* at the same time, as it may appear. Of course, I'm assuming that your page contains a `Language="C#"` directive, so that the `<script runat="server">` doesn't need to specify the language it's written in. Good luck.
Humberto
Nope. This just gives me around 10-12 errors.I'm going to just post a new question asking for an answer in VB.
BioXhazard