Results 1 to 6 of 6

Thread: [RESOLVED] Using AJAX / net2.0 / Cant setFocus on textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    58

    Resolved [RESOLVED] Using AJAX / net2.0 / Cant setFocus on textbox

    Hey guys,

    First my environment is using AJAX and .net 2.0 framework. I have a button that toggles between showing a dropdown or a textbox. Basically the button is clicked, if the dropdown is visible, hide it and show the textbox and vice versa...

    My issue is neither one of these code options are working.
    1) page.SetFocus(txtInsertTime) doesn't work.
    2) System.Web.UI.ScriptManager.RegisterClientScriptBlock _
    (Me.txtInsertTime, Me.GetType(), "FocusOnTimeTB", "document.form1['txtInsertTime'].focus();", True)

    I think its something do to with the AJAX and that the control becomes
    visible after the script .setfocus happens...

    In code I do set Me.txtInsertTime.Visible = True before I call page.SetFocus(txtInsertTime). And, I'm using page.SetFocus() other places but the controls are visible all the time...

    Anyone have a solution, another way?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Using AJAX / net2.0 / Cant setFocus on textbox

    If the control does become visible later in the page's client side life cycle, you could then use RegisterClientScriptBlock, but do a

    window.onload = (your js code to set focus);

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    58

    Resolved Re: Using AJAX / net2.0 / Cant setFocus on textbox

    Didn't work, thanks for trying. I figured it out tho... and will share it with others whom are new to AJAX.

    IF the control is in an UpdatePanel you must use the:
    scriptmanager.setfocus() and not the page.setfocus()

    ex. ScriptManager1.SetFocus(TextBox2)

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Using AJAX / net2.0 / Cant setFocus on textbox

    That renders out to the same thing

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    58

    Re: [RESOLVED] Using AJAX / net2.0 / Cant setFocus on textbox

    absolutely! just as i was debugging, I used js on the aspx to set focus on a different textbox, one thats not in a updatepanel and i actually saw the cursor set focus to that textbox, so i knew my js code was working properly. However thereafter as the new txtTime textbox became visible, the focus was removed!

    so just a FYI - scriptmanager.setfocus is the way to actually set focus on a control inside a updatepanel

    It very well could be the way my code is written, but no matter which way i did it... actual JS function on the aspx page, JS registerscriptblock, or page.setfocus, it wouldnt set focus. So, I'm assuming here that in end the scriptmanager must get the last say, as its rendered to the client...
    Last edited by Eclipsyo; Sep 24th, 2008 at 11:30 AM.

  6. #6
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Re: Using AJAX / net2.0 / Cant setFocus on textbox

    Thank u
    it has helped me...
    ScriptManager1.SetFocus(TextBox2)[/QUOTE]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width