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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set the focus on a text field 6

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
I want to set the focus on the first Text field of my form but don't know how to do as Setfocus does not exists anymore.

Thanks for your help.
 
Two methods
1. Setup the Tab index's for your controls so that the one that needs focus is 0 next is 1 next is 2... and so on

2. Use some client side javascript to do the job. Link 9 wrote some nice functions that you should be able to use. You can find them here. thread855-292711

HTH That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Or you could do this:

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)

Dim s As String = &quot;<SCRIPT language='javascript'>document.getElementById('&quot; & ctrl.ID & &quot;').focus() </SCRIPT>&quot;

RegisterStartupScript(&quot;focus&quot;, s)

End Sub

which is a reusable sub that allows you to set the focus of whatever control you pass in.

D'Arcy
 
fine then Jacko be like that.. just pop in with your superiour javascript knowledge.

anywhoo I like Jack's solution better. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
A-HA! see...you added an o to your inferior version of &quot;anyhoo&quot;!

I'm converting you slowly but surely!
;)

D

ps thanks for the props
:)
 
doh!
I will not fall into the shadow
I will not fall into the shadow
I will not fall into the shadow

Here un pretty star for you too. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Yo Jack what namespace did you import to use RegisterStartupScript?
Or have I finally reached that land of pink and purple poka dots? That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
uh...you've reached the land of pink and purple poka dots...or more accurately, the land of the black and blue western conference wannabe's, cause HER COME THE BOMBERS! OH YEAH!

ok, now that thats been said...

I didn't have to import anything to my knowledge, Mark. I found that code over a year ago online, and just used it directly in our project.

I'll check work on Tuesday and give you a difinitive answer, but right now, I don't think there's anything that needs to be imported

D'Arcy
 
I used your SetFocus function and... it's working perfectly !!!
Many thanks.
 
Funny cause VS gets mad at me when I try to type RegisterStartupScript That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I submit myself to one public beating.

The intellisense won't bring it up but I get no error and it works. Now to see if I can't get it working in my user controls, because there it is giving me an blue squiggly.

aha.. You must add Page.registerStartupScript.

For anyone who is interested you need to make a small tweak to get the function to work from within a user control.

Public Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim sb As New StringBuilder()
sb.Append(&quot;<SCRIPT language='javascript'>document.getElementById('&quot;)
sb.Append(Me.UniqueID)
sb.Append(&quot;:&quot;)

sb.Append(ctrl.ID)
sb.Append(&quot;').focus() </SCRIPT>&quot;)

Page.RegisterStartupScript(&quot;focus&quot;, sb.ToString)

sb = Nothing
End Sub


The bolded lines must be added as &quot;ControlID:&quot; is added before the control when it gets to the browser. If you didn't understand what I said then just copy and trust.

Over and out! That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
[rofl2] thanks jack That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Trying this, I got no error message:

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = &quot;<SCRIPT language = 'javascript'>document.getElementById('&quot; & ctrl.ID & &quot;').focus()</SCRIPT>&quot;
RegisterStartupScript(&quot;focus&quot;, s)
End Sub
 
uh how is that different than Jacks solution?
As I said above I did get this working anyway twas just a silly mistake. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I have a question about passing the control to this sub. Do you do it on the page or in another sub. Right now when I send it from another sub nothing happens?

Scott

 
I pass the control from a sub, not from the page (by page, I'm guessing you mean the html view?)

Can you write your code that calls the SetFocus sub? Just curious to see how you're passing in your control

D
 
SetFocus(responsibilites)

responsibilites is the id of the textbox of course
 
The ID or the name?

i.e.

If you looked at all the declarations in your code behind would you see

Protected WithEvents responsibilities As System.Web.UI.WebControls.TextBox

?

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top