Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus in UpdatePanel 1

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
GB
I have an UpdatePanel with a few textboxes. If the value in one textbox is less than the value in another the background turns pink and a hidden textbox (held in a placeholder) is shown.

Code:
    Protected Sub txtAccepted_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAccepted.TextChanged
        If Not txtQuantity.Text Is Nothing AndAlso Convert.ToInt16(txtQuantity.Text) > 0 Then
            If Convert.ToInt16(txtAccepted.Text) < Convert.ToInt16(txtQuantity.Text) Then
                plhNCR.Visible = True
                rvalNCR.Enabled = True
                txtAccepted.BackColor = Drawing.Color.LightPink
                txtNCR.Focus()
            ElseIf Convert.ToInt16(txtAccepted.Text) > Convert.ToInt16(txtQuantity.Text) Then
                txtAccepted.Text = txtQuantity.Text
                plhNCR.Visible = False
                rvalNCR.Enabled = False
                txtAccepted.BackColor = Drawing.Color.LightGreen
                drpOperator.Focus()
            Else
                plhNCR.Visible = False
                rvalNCR.Enabled = False
                txtAccepted.BackColor = Drawing.Color.LightGreen
                drpOperator.Focus()
            End If
        End If
    End Sub

This all works but the issue I'm having is that I'd like to ensure the focus after the partial postback is set to the next control on the page so that the user can continue to tab through the fields. Currently the focus is lost and you have to mouse click in the next textbox to continue entering data.

Is there a relatively quick way I can achieve this?

Steve G (MCSE / MCSA:Messaging)
 
use the ScriptManager.RegisterStartupScript() to set focus of the desired control. something like this (haven't tested, so may need some tweaking)
Code:
string js = string.Format("$('{0}').focus();", MyControl.ClientID);
ScriptManager.RegisterStartupScript(this, typeof(this), "SetFocus", js, true);


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks. I've tried various ways of using that and I can't get it to work. It doesn't cause any errors but it still won't set the focus on any control in the update panel.

I adjusted it to the following and put in in page_load as a test but still no joy, even on the initial load.

Code:
Dim js As String = String.Format("{0}.focus();", txtQuantity.ClientID)
        ScriptManager.RegisterStartupScript(Page, Me.GetType, "SetFocus", js, True)


Steve G (MCSE / MCSA:Messaging)
 
your code wouldn't work because it would produce the following js on the client
Code:
ctl00_txtQuantity.focus();
since you haven't defined what ctl00_txtQuantity is js would fail.

if the ajax library scripts haven't loaded then [tt]$('ctl00_txtQuantity').focus();[/tt] would probally fail as well.

after experimenting with the onload js event I found this does not work
Code:
<html>
   <body onload=[COLOR=red]document.getElementById('mytextbox').focus();[/color]>
      <form><input id="mytextbox" type="text" /><form>
   </body>
</html>
But calling functions does work. try this
Code:
//aspx
<head>
   <script>function setFocus(id){document.getElementById(id).focus();}</script>
</head>

//Code Behind
Dim js As String = String.Format("setFocus('{0}');", txtQuantity.ClientID)
ScriptManager.RegisterStartupScript(Page, Me.GetType, "SetFocus", js, True)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I put the script function on my master page and tried the code you suggested in the code behind (Page_Load) but it still doesn't work. The update panel refreshes but the focus doesn't not appear in any of the textboxes.

Is the page load the right place to put the code?

Steve G (MCSE / MCSA:Messaging)
 
the code should be placed in the TextChanged event, not the page load.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for all of your help Jason. I still could not get that to work but by chance stumbled upon this code that does seem to work...

System.Web.UI.ScriptManager.GetCurrent(Me).SetFocus(Me.txtNCR)

Steve G (MCSE / MCSA:Messaging)
 
interesting... I didn't think the System.Web.UI.ScriptManager worked like this with UpdatePanels.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top