2 Replies - 903 Views - Last Post: 04 April 2019 - 06:12 AM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,170
  • Joined: 12-January 10

is there a way to set focus on a text box on postback --->

Posted 04 April 2019 - 05:22 AM

is there a way to set focus on a text box on postback without putting javascript on the html page?

example i have 5 tabs all with dynamically controlled text boxes that can range from 1 text box per tab to thousands. the text boxes all have post back events for math purposes ---- is there a way to keep the focus or advance focus to the next text box after postback without embedding javascript to the html page?


they do not like embedded js----

every example i have found all use embedded js

Is This A Good Question/Topic? 0
  • +

Replies To: is there a way to set focus on a text box on postback --->

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15171
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: is there a way to set focus on a text box on postback --->

Posted 04 April 2019 - 06:01 AM

Not sure how the textboxes are labeled but in older ASP.NET 4.0 etc.. you could do 'controlName.focus'.. if your controls are numbered break apart the name, increment the number, and then try calling that.
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,170
  • Joined: 12-January 10

Re: is there a way to set focus on a text box on postback --->

Posted 04 April 2019 - 06:12 AM

all the controls are dynamically created

ive tried the following:

sub SetFocus(ByVal ctrl As Control) 
        Dim sb As New System.Text.StringBuilder("") 
        with sb 
            .Append("<script language='Javascript'>") 
            .Append("function SetFocus()") 
            .Append("{") 
            .Append("document.") 
            .Append(ctrl.Parent.ID) 
            .Append("['") 
            .Append(ctrl.UniqueID) 
            .Append("'].focus();") 
            .Append("}") 
            .Append("window.onload = SetFocus;") 
            .Append("") 
            .Append("</script") 
            .Append(">") 
        end with 
        ctrl.Page.ClientScript.RegisterClientScriptBlock([GetType](),"SetFocus", sb.ToString()) 
    end sub 



in text change event
Session ("SenderFocus") = sender


in page load event
 If IsPostBack Then
           if Not Session("SenderFocus") Is Nothing 
             SetFocus(Session("SenderFocus"))
           end if
       End If

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1