Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the Focus to a textbox on button click

Status
Not open for further replies.

LauraCairns

Programmer
Jul 26, 2004
173
GB
Does any one know how I set focus in a textbox after the onclick of a button using code in the code-behind page. Im using c#. There are 4 textboxes on the page like so: -

txtSurname = tabindex(1)
txtForname = tabindex(2)
txtHouseNo = tabindex(3)
txtPostcode = tabindex(4)

I need the focus to be set on txtHouseNo when the button on the page is clicked.

I have the following page load event incase code needs to be added in here for this to work. Can anyone please help me.

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList(5);
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
}
}
}
 
use...
Code:
Response.Write('<script language="JavaScript">document.getElementById('txtHouseNo').focus();</script>');

or
Code:
Response.Write('<script language="JavaScript">document.forms[0].txtHouseNo.focus();</script>');


--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
When I try and use either of these lines of code I seem to be getting a build error: -
'Too many characters in character literal'
Do you know how to solve these. Is there some sort of syntax error.

Do I add this code to the onclick event of a button??
 
oopps... replace ' with " (sorry)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Code:
Response.Write("<script language='JavaScript'>document.forms[0].txtHouseNo.focus();</script>");

or
Code:
Response.Write("<script language='JavaScript'>document.getElementById('txtHouseNo').focus();</script>");

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Tride that and got the following interent explorer script error: -
'document.forms.0.txtHouseNo' is null or not a object

Have you any ideas what is wrong.
Im running the page in VS by right-clicking and view in browser. I have to run the page this way as we are having trouble building and running the solution at present.
 
use the other one:
Code:
Response.Write("<script language='JavaScript'>document.getElementById('txtHouseNo').focus();</script>");

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Tried that and now got the following interent explorer script error: -
'document.getElementById(...)' is null or not a object

I had the txtHouseNo wrong before it is txtHouseNum. Changed it but still give me an error. Do I need to add in the form name or anything??
It is called the following.
<form id="Template" method="post" runat="server">

Response.Write("<script language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
 
hmmm... i know what's going on...
this statement should be written on page after the textbox. but it's not.

use this instead:
Code:
LiteralControl li = new LiteralControl("<script language=JavaScript>document.getElementById('txtHouseNum').focus();</script>");
Page.Controls.Add(li);

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks thats brilliant!! It worked a treat!! Would never have worked that out myself.
 
if you want to do this on the click of the button, do this

Button1.Attributes.Add("onclick", "document.getElementById('txtHouseNum').focus();");

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
this would save a roundtrip to the server since it's all done on client side

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
yeah that seems like a really good idea. Do I put this at the top of my html code inside Javascript and then change the name of Button1 to the appropriate button??
 
does this button have to do anything else beside giving focus to the textbox?

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
yeah it has to do quite a bit more. If it has to go to the code behind page any way is there much point doing the focus thing client-side
 
exactly... there's no point in doing it client side plus it will have no effect.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top