Focus on control in webform

  • Thread starter Thread starter reidarT
  • Start date Start date
I tried this with two rich text boxes. When the program is started, it
would automatically give focus to richTextBox1. So in order to give
focus to richTextBox2, in the form load method, i called the following:

this.richTextBox2.Select();

It worked for me.
 
In javascript:

document.getElementById('FirstName').focus();

If you wanted to do this in your server side code then you need a
procedure that registers this script:

public void SetFocus(Control focusControl)
{
string clientID = focusControl.ClientID;
System.Text.StringBuilder script = new
System.Text.StringBuilder();
script.Append(@"<script language='javascript'>");
script.Append(@"if(document.getElementById('" + clientID +
@"')){document.getElementById('" + clientID + @"').focus();}");
script.Append(@"</script>");

RegisterStartupScript("setFocus", script.ToString());
}

Then all you have to do is call it:

SetFocus(this.FirstName);

David Barkol
www.neudesic.com
 
Page.RegisterStartupScript("focusScript", @"<script>

function setFocus(elementID) {

element = window.document.getElementById(elementID);

element.focus();

}

setFocus('txt1');

</script>");



HTH



DalePres

MCAD, MCDBA, MCSE
 
Back
Top