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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Losing Focus upon Postback to URL window 1

Status
Not open for further replies.

ALWilliams

Programmer
Jun 29, 2007
21
0
0
GB
Firstly I am new to ASP so apologies if i have missed the obvious here!!!

I have a page with two text boxes on it. The idea is for upon initial load the focus is on box1. I am after scanning a barcode into this box which I want it to navigate away upon finding the CRLF from the barcode reader. It should then run some code which will populate some labels with info and then finally set the focus to the second textbox ready for a job barcode scan.

Upon this second scan, this will then execute some other code that will read a db and populate more labels ready for the user to select the status of this job and press a button which will update the db of such.

I have it currently so that the page loads and correctly focus' to textbox1. However, upon losing focus from this box (currently by hitting TAB) it kicks the postback routine and upon returning sets the focus to the URL of my page and not the second textbox even though I have it set as the next item in the tabindex list.

I've tried an if statement which kind of says if not postback set focus to box 1, else box 2 yet it still ignores the second box's setfocus.

Any ideas appreciated!
 
You will have to post some code. We have no idea what you are trying
 
tab index doesn't apply between requests. you are sending a new request (postback in webforms terms).

I think there is a SetFocus method on webcontrols. if there is this code may work
Code:
public void Page_Load(object sender, EventArgs e)
{
   if(IsPostback)
      TextBox2.SetFocus();
   else
      TextBox1.SetFocus();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thats pretty much exactly what I have but when it postsback the focus is on the URL line and not the second textbox for some reason.

I've amended it to exactly your code and its now working!!!

Damn computers!!!

Thanks.
 
Here's a SetFocus method that uses JavaScript that I use to set the focus to any control. It puts the control at the top of the window also.

//This method will set the focus to the desired control with the
//control at the top of the window.
public void SetFocus(Control ctrl)
{
// Define the JavaScript function for the specified control.
string focusScript = "<script language='javascript'>" +
"window.scrollTo(0,document.body.scrollHeight); " +
"document.getElementById('" + ctrl.ClientID +
"').focus();</script>";

if (!((Page)HttpContext.Current.Handler).ClientScript.IsStartupScriptRegistered("FocusScript"))
((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(focusScript.GetType(), "FocusScript", focusScript);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top