Link to home
Create AccountLog in
Avatar of TeknoBearSF
TeknoBearSFFlag for United States of America

asked on

set focus on the first textbox on the page with ajax editor in .net

I a have a web form with few textboxes, dropdowns and finally towards the end of the page there are few custom ajax editors. So on page load the focus is always inside the last editor and no way it comes to the first text box or top of page.On each page load the cursor goes inside the last editor control.How to bring the focus inside the first text box? Tried many things and still do not work.

Any ideas, thanks.. (This is a direct 500 points question).
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx

Try to set the focus of the text page on pageload and see if that bring the focus to the textbox which you are trying to set focus.
Using jQuery you can try this code:

$(document).ready(function() {
  $('input:text:first').focus();
});

Open in new window


or using plain Javascript as:

window.onload = function() {
  document.getElementById("TextBox1").focus();
};

Open in new window

Setting the focus with script is necessary for IE.  Good browsers support:

<input autofocus="autofocus">
 

Cd&
Avatar of TeknoBearSF

ASKER

@lucky85: Thanks for the response ,i  tried the ryan farley's method and i can see that first the control is there in the required text box and then finally it goes back to editor.

@rpkhare:Thanks for the response. I had already checked the javascript one and the jquery  one is also not working.

For the ryan farley method and the jquery one, i found initially the control is in the required textbox and then it shifts down to the editor.
@Cd:Thanks for the reply.
I am using IE8 and it gives me an error telling "Autofocus" is not a valid attribute.
for ex: if you just use only 2 controls a textbox and ajax editor in a page ,the focus is always inside the editor .  :(
Try using script in below URL. This URL should help you to set the focus on textbox even you have ajax editor.

Sample code:

http://vincexu.blogspot.com/2009/06/keep-textbox-focus-inside-updatepanel.html
Thanks lucky85 for the suggestion.That also didn't work.

But i found a solution... :)
Cool. If possible will you be able to share your solution so that everyone can learn from your post.
ASKER CERTIFIED SOLUTION
Avatar of TeknoBearSF
TeknoBearSF
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
It worked for me.