Able
Able

Reputation: 3912

ModalPopupExtender setfocus issue

I'm using a Ajax ModalPopupExtender to show a popup panel. The panel has a TextBox. I want to setfocus to that TextBox while popup the panel. Is there is any method or mechanism to set the focus to the popup extender. I have tried many ways to achieve this, but failed. Please help to resolve this issue.

Upvotes: 2

Views: 3918

Answers (4)

Eric Kristiansen
Eric Kristiansen

Reputation: 11

rkw 's answer among others helped me derive a convoluted fix. I had to use a timeout and a call to another function in order to properly place the focus in my modal textbox. Less convoluted solutions did not work for me. I believe, the addition of the setFocus() allowed for the proper execution queue to my desired end. ...hope this helps someone else. Like rkw suggested, only a millisecond was necessary to accomplish the task.

//markup

                    <asp:Button ID="btnShow" runat="server" Text="Add New Test" OnClick="btnShow_OnClick" OnClientClick="return modalAdjust()"  /> 

//javascript

  function setFocus() {
              try {
                  document.getElementById('<%= TextBox_TestDescription.ClientID %>').focus();
              } catch (e) {
                  alert(e);
              }
          }

          function modalAdjust() {
              try {
                  setTimeout("setFocus();", 1);
              }
              catch (e) {
                  alert(e);
              }
          }

Upvotes: 0

rkw
rkw

Reputation: 7297

Since you have tried many methods without any success, there is a good chance the focus code is executing a slight bit before the panel and text box exists.

setTimeout('document.getElementById("TextBox").focus();',1);

Upvotes: 1

IUnknown
IUnknown

Reputation: 22468

Add the javascript below:

function pageLoad() {
    $find('modalPopupBehaviorID').add_shown(function () {
        $get("<%= TextBox1.ClientID %>").focus();
    });
}

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

On opening popup:

document.getElementById("TextBox").focus();

Hope this helps (and hope not to have been too obvious hehe)

Upvotes: 0

Related Questions