Solved

Restore focus after postback

Posted on 2015-02-07
5
121 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
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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