Setting focus on javascript triggered postback

I am working with ASP.NET working with client side javascript.
I have the following javascript to handle the XMLHTTPRequest callback. In certain situations, the page will be posted back using the __doPostBack () function provided by ASP.NET, specified in the code below. However, I would like to be able to adjust focus to the dropdown controls after the back message appears. Is there a way to set this using Javascript, or do I need to tweak it in some other way.

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

      

0


source to share


2 answers


i found a solution for this. In the code that calls the event handler, which is called for each specific element, I call Control.Focus () as the last line. For example, if the dropdownlist event handler is triggered and the next focus control is a 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 for me than I tried. I keep trying to complicate things by creating Javascript on the fly that does exactly what Microsoft does for me in the Framework.

0


source


Since you are doing a full postback , you will need to use Page.SetFocus on the server side to get the appropriate JavaScript emitted when the next page is loaded.



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

+3


source







All Articles