Which control had focus before postback?

  • Thread starter Thread starter Marco Liedekerken
  • Start date 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
 
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.
 
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.
 
Back
Top