Solved

Restore focus after postback

Posted on 2015-02-07
5
122 Views
Last Modified: 2015-06-07
Hello,

I am not able to keep the focus on my textbox after the postback.
After postback, my focus goes to the URL automatically.

Here is what I have in my cs code:
  protected void Page_Load(object sender, EventArgs e)
    {
     
        Tbx_VIN.Focus();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Label4.Text ="VIN scanned:" +Tbx_VIN.Text;
        SqlDS_barcode.Insert();
        Tbx_VIN.Text = "";
        Tbx_VIN.Focus();
    }
    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        Tbx_VIN.Focus();
    }

Thanks
0
Comment
Question by:arnololo123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40595587
 protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack) 
        {
           // Response.Write("IsPostBack");
           Tbx_VIN.Focus();
        }
        else
        {
           // Response.Write("Not IsPostBack");
        }
    }

Open in new window

0
 

Author Comment

by:arnololo123
ID: 40595644
Thanks but it does not work.
I forget to mention that I am using a scanner to scan a barcode.
It seems that the problem only occur if I use the  barcode scanner, if I just type something and click away, the focus will stay.

Regards
0
 
LVL 5

Expert Comment

by:Greg Gamble
ID: 40596552
If this is an HTML5 project just add the parameter 'autofocus' to the control.  It might show as an error in the IDE, but it'll be fine and will validate .
0
 

Author Comment

by:arnololo123
ID: 40673082
This is not HTML5
0
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
ID: 40676894
One the client side(javascript), you can set the focus. Maybe "document ready" or "onload" events
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question