Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a login.aspx page with a textbox and button inside an UpdatePanel. I've wired a JS function to the textbox's onkeydown event so that if the user hits enter it will click the button
function clickButton(button)
{
    if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13))
    {
        document.getElementById(button).click();

        return false;
    }
    else
    {
        return true;
    }
}

this works(the button's click event fires on the server). The click event checks the value in the textbox then does Response.Redirect("Default.aspx"); when that line executes the Login.aspx's Page_Load event fires again. I've stepped through this a few times and cant see why. However if a user clicks the button with the mouse everything works perfectly. I put breakpoint in Default's Page_Load and it never got hit so i'm pretty sure it isnt that page redirecting back to Login.

I'm obviously doing something wrong, but cant seem to figure out what it is or how to fix it. I've use the js function before and havent had any problems.

Any help would be appreciated.
Posted
Comments
[no name] 13-Apr-11 10:07am    
The first question is why are you not using the build-in ASP.NET login controls or just setting the button as the default?
SomeGuyThatIsMe 13-Apr-11 10:25am    
It was simple enough to do w/out using a built in control so i never thought about it. The default button hadnt worked the first time i tried it, now it plus something else i've changed make it work now.
Karthik. A 13-Apr-11 10:46am    
So when/where do you call this js function? 'OnClientClick' of the asp:Button ? Can you paste that part?

Adding the defaultButton property to the form worked when i tried it again. It hadnt earlier to i resorted to the JS function. I'm not sure what changed that made it work now, i'm trying to figure it out. If i do i'll update the question. Thanks for the help.
 
Share this answer
 
Try with following code,-
function checkKeycode(e) {
var keycode;
if (window.event)
{ keycode = window.event.keyCode;
}
else if (e)
{
keycode = e.which;
}

if (keycode == 13)
{
__doPostBack('btnSubmit', "");
}
}

</script>

or,
Make sure breakpoint is working in button click event
 
Share this answer
 
Try placing your button outside the update panel and see whether it works...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900