Click to See Complete Forum and Search --> : Focus on webforms


Dozo_1st
September 9th, 2004, 07:52 AM
How do I put the focus on a textbox on a webform?
After a postback, the webform is reloaded and the focus on the textbox is lost.

e.g. I have 2 textboxes: txt1 and txt2.
txt1 has AutopostBack = true and Index=1
txt2 has Index=2

After using the TAB-key, the PostBack is necessary for updating some objects on the webform. But when the page is reloaded by the autopostback, the focus is lost and the cursor does not reach txt2. :eek:

How do I get the focus on txt2? And how do I do this if I have more controls on the same form which also have autopostback?

Thank you very much in advance!

Dozo

ABudair
September 11th, 2004, 07:10 AM
Dear,

Try this at code behind:

void Set_Focus()
{
string strScript ;


strScript = "<script language=javascript> document.forms[0].pageName_txtName.focus(); </script>";
Page.RegisterStartupScript("focus", strScript);
}

in Case you have a user control into page and your text box placed into this user control, replace the pageName with your ControlName.

for example
document.forms[0].login_txtUserName.focus();

hope it helps

bye..

ABudair
September 11th, 2004, 07:11 AM
Dear,

Dont forget to invoke setFocus() method into Page_Load()

private void Page_Load ()
{

if (!IsPostBack)
this.setFocus();
}

good luck again, bye...

Dozo_1st
September 14th, 2004, 10:30 AM
Thanks!!! I'll try that!

Dozo