views:

275

answers:

2

I am working with ASP.NET doing some client side javascript.
I have the following javascript to handle an XMLHTTPRequest callback. In certain situations, the page will be posted back, using the __doPostBack() function provided by ASP.NET, listed in the code below. However, I would like to be able to set the focus a dropdownlist controls after the post back occurs. Is there a way to set this using Javascript, or do I need to rig that up some other way.

    function onCompanyIDSuccess(sender, e) {
       if (sender == 0)
           document.getElementById(txtCompanyIDTextBox).value = "";
       document.getElementById(txtCompanyIDHiddenField).value = sender;
       if (bAutoPostBack) {
           __doPostBack(txtCompanyIDTextBox, '');
       }
   }
+1  A: 

Since you're doing a full postback, you'd need to use Page.SetFocus on the server side to get the appropriate JavaScript emitted on the next page load.

Otherwise, in a pure AJAX solution - document.getElementById('id').focus() would do the trick.

Mark Brackett
+1. You prove what makes SO such a fantastic resource on the web.
PEZ
A: 

i have found the solution for this one. In the code behind event handler being called for each particular item, I call the Control.Focus() as the last line. For instance, if a dropdownlist event handler is being triggered, and the next control to get focused is the zipcode text box:

protected void ddl_state_selectedValueChanged(Object sender, EventArgs e)
{
    // ... here is all my code for the event handler

    txtZipCode.Focus();
}

It was much easier that I what I was trying to do. I keep trying to overcomplicate things by creating Javascript on the fly that does exactly what Microsoft is already doing for me in the Framework.

stephenbayer