Link to home
Start Free TrialLog in
Avatar of scm0sml
scm0sml

asked on

setting focus after tabbing off a textbox in an update panel

Hi,

I have a number of controls inside an update panel.

When I tab off the first which has an autopostback=true, I run some code that goes off an hits the db, then sets the value of a literal control based on the results.

The problem is when I tab off, initially the cursor goes to the next textbox, however once the db hit has finished and the value of the literal gets set, the 2nd textbox then loses focus and the cursor is no longer where it should be.

I have tried adding txtInvCP.Focus() which is the 2nd textbox, inside the function that runs on the autopostback but still no joy, hopefully there is a way around this?

Thanks in advance.
Avatar of Akin Delu
Akin Delu
Flag of Nigeria image

some little javascript trick will do that for you.

<script type="text/javascript">
function RegisterEvent()
{
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}
function endRequestHandler(sender, args)
{
    document.getElementById("<%= txtInvCP.ClientID %>").focus();
}
</script>

<body onload="RegisterEvent()">
Avatar of scm0sml
scm0sml

ASKER

wont that mean everytime the page loads the one specific textbox will be given focus?
yeah thats true, then set the RegisterEvent() function in the onchange event of the textbox that will trigger the database call

sample
==========

<asp:TextBox ID="TextBox3" onchange="RegisterEvent()" runat="server" AutoPostBack="True" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>
Avatar of scm0sml

ASKER

seems as if the whole page is losing focus?

Your solution didn't work until I randomly put an alert('test'); before it, then once I have clicked the ok....the next textbox has focus...

Whatever this problem is is probably the reason it wasn't doing it initially does that sound right?

Any ideas?
Avatar of scm0sml

ASKER

Sorry when I say before it I mean:
 function endRequestHandler(sender)
        {
        alert('HEY1');
            document.getElementById('ctl00_maincontent_ucInvoice_ucInvoiceQuery_txtInvCP').focus();
           
        }
ASKER CERTIFIED SOLUTION
Avatar of scm0sml
scm0sml

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scm0sml

ASKER

Had no answers to fix this but found a solution myself.