Which control had focus before postback?

  • Thread starter Marco Liedekerken
  • Start date
M

Marco Liedekerken

Hi,

Is it possible to retrieve the control that had the focus when the page was
posted back?

Because the focus is lost when a postback occurs I want to manually set the
focus to the control that previously had the focus (smartnavigation doesn't
do the trick).

Thanks, Marco
 
K

Kairi Zikpin

use javascript
once the page hits the browser, .net is no longer at play

eg, object.onfocus(), set some hidden field to the control's name
once the page is back to the browser, use javascript again to set focus

it's a lot of work but that's the only way i've found that works. just
be careful about objects no longer being on the form or being disabled.
 
J

Jeff Siver

Here's a C# function you can call to have a page set focus when the page is
displayed:

protected void SetFocus(WebControl wc)

{

StringBuilder sScript = new StringBuilder("");

sScript.Append("<script language='javascript'>");

sScript.Append(" document.getElementById('" + wc.UniqueID + "').focus()");

sScript.Append("</script>");

Page.RegisterStartupScript("Focus", sScript.ToString());

}

All you need to do is call this function before posting the page. Should do
what you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top