Setting the focus on control jumps page to the top
-
Hi, My senario: I have a sign-up form which, amoungst others, has a username textbox. The username textbox has autopostback set to true which, when posted back, checks to see if the entered username already exists in the DB and displays a message if so. This works fine. But the problem I have is that in the code behind I am setting the focus to a control after doing the username check ( using txtUsername.Focus() ). But doing this causes the page to jump back to the top. This doesn't happen if I don't give a control the focus. Any ideas how I can solve this? A little more info about my form just in case it's relevant. The form is in an AJAX Accodion control which is in an AJAX Update Panel. Thanks :)
-
Hi, My senario: I have a sign-up form which, amoungst others, has a username textbox. The username textbox has autopostback set to true which, when posted back, checks to see if the entered username already exists in the DB and displays a message if so. This works fine. But the problem I have is that in the code behind I am setting the focus to a control after doing the username check ( using txtUsername.Focus() ). But doing this causes the page to jump back to the top. This doesn't happen if I don't give a control the focus. Any ideas how I can solve this? A little more info about my form just in case it's relevant. The form is in an AJAX Accodion control which is in an AJAX Update Panel. Thanks :)
Why dont you use
document.getElementById('textbox').focus();
in onload of the page ?? :confused:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Why dont you use
document.getElementById('textbox').focus();
in onload of the page ?? :confused:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using JavascriptWouldn't that set the focus to that particular textbox every time the page was loaded? I need to be able to set the focus to a different textbox depending on the result of the username check, i.e. if the check fails then set focus to txtUsername but if it passes then set focus to txtPassword. But also, when the page is loaded for the first time then the focus needs to be set to yet a different control. So using Javascript how would I determine which control to give the focus to? Thanks for help by the way :)
-
Wouldn't that set the focus to that particular textbox every time the page was loaded? I need to be able to set the focus to a different textbox depending on the result of the username check, i.e. if the check fails then set focus to txtUsername but if it passes then set focus to txtPassword. But also, when the page is loaded for the first time then the focus needs to be set to yet a different control. So using Javascript how would I determine which control to give the focus to? Thanks for help by the way :)
Well, if the page is actually posted back, you might consider adding a script block using
RegisterScriptBlock
to the client page to have the proper textbox focus. It is not usually great to do focus in server side in case of ASP.NET. use txtBox.ClientId to get the id on the control to be focus. But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not. It is really unnecessary to fully postback the page for this. ;)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Well, if the page is actually posted back, you might consider adding a script block using
RegisterScriptBlock
to the client page to have the proper textbox focus. It is not usually great to do focus in server side in case of ASP.NET. use txtBox.ClientId to get the id on the control to be focus. But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not. It is really unnecessary to fully postback the page for this. ;)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using JavascriptAbhishek Sur wrote:
But If I was in your situation, I would have invoked an AJAX call to the server, and get if it is available or not.
Ah, didn't realise that I could do this (I am a newbie to ASP.NET :D ) Thanks again for your help.