0

I believe the title says it all. How do you get focus on a control inside FormView within InsertItemTemplate within a Table from javascript?

The asp code looks like this

<asp:Panel ...>
 <asp:UpdatePanel ...>
  <ContentTemplate>
   <asp:SqlDataSource ...>

    <asp:FormView ID=FormView1 ...>

      <InserItemTemplate>
         <asp:Table ID=tblIns ...>
           <asp:TextBox ID=txtInsMyBox ....>
           </asp:TextBox>
         </asp:Table>
      </InserItemTemplate>

      <EditItemTemplate>
         <asp:Table ID=tblUpd ...>
           <asp:TextBox ID=txtUpdMyBox ....>
           </asp:TextBox>
         </asp:Table>
      </EditItemTemplate>

    </asp:FormView>

    <asp:ValidationSummary />
    <div>
    <asp:Button ID="btnSave" .../>
    <asp:Button ID="btnCancel" .../>
    </div>
  </ContentTemplate>
 </asp:UpdatePanel>
</asp:Panel>

And the javascript code which is in the header looks like this:

<script type="text/javascript">
     function regainFocus() {
          //this works
          window.parent.document.getElementById("btnSave").focus();
          //this also works
          window.parent.document.getElementById("FormView1").focus(); 
          //this does not work
          window.parent.document.getElementById('<%#FormView1.FindControl("txtInsMyBox").ClientID%>').focus();
     }
</script> 

I have tried changing the tag from "#" to "=" sign and it made not difference. I have also tried to find other different controls inside the FormView with no success.

Can somebody shed some light on how to FindControl of an object inside a Table inside a FormView?

user2912964
  • 33
  • 1
  • 8
  • Is there only one `txtMyBox` text box on the page? – Greg Burghardt Jul 09 '15 at 21:19
  • There are 2 txtMyBox 'es in the program. One for update and the other for Insert. But they are named txtInsMyBox and txtUpdMyBox. They are named something different in my program: txtupdDxxSoooLeeeeeHooooDoooooo. Is there a restriction on how long the names can be in length? – user2912964 Jul 09 '15 at 21:26
  • I finally got it to work, but not by using the focus in the javascript. It seems to me that only a button can be focused by a javascript, so I placed a hidden button, called btnSetFocus which could then set focus to the objects in the formview. – user2912964 Jul 15 '15 at 20:14
  • You can set focus to text boxes. That shouldn't be a problem. – Greg Burghardt Jul 15 '15 at 21:00

0 Answers0