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

Give focus to an asp:textbox control 2

Status
Not open for further replies.

kyledunn

Programmer
Jan 2, 2001
145
US
I'm using an asp:textbox control on a web form that includes several asp:textbox controls and an asp:button control. No controls initially have the focus. Anyone able to tell me how to give the asp:textbox the focus so the cursor will automatically be blinking in the textbox once the page is loaded?
 
Hey Kyle,

The only way I know of doing this is through client side scripting. So on your page, you'll have to add something like this:
<script language=vbscript>
sub Window_OnLoad
document.Form1.txtYourTextBox.Focus
end sub
</script>

If you're using visual studio.net, you just switch to the html veiw of your page, and you can either type it in manually, or right click on your html code, select &quot;insert code block&quot;, then &quot;client&quot;, and it'll give you the template for javascript.

Hope this helps,
Jack
 
Thanks Jack. I did come to the same conclusion and used the onload in the body tag to run a java script. Thanks for the help.
 
Hey Kyle,

Found another way to do it. It basically does teh exact same thing, and at first I wasn't really sure if it had any advantage. But after I thought about it, it kinda does. Basically, you can write function that will accept a control and passback the javascript to set focus to the page. I have a C# DLL that has this function (which a friend sent me from dotnetmonkeys(?)):

using System;
using System.Web.UI;

namespace ToolBox
{
public class PageToolbox
{

public static void SetInitialFocus(Control control)
{
if (control.Page == null)
{
throw new ArgumentException(&quot;The Control must be
added to a Page before you can set the IntialFocus
to it.&quot;);
}
if (control.Page.Request.Browser.JavaScript == true)
{
control.Page.RegisterClientScriptBlock
(&quot;InitialFocus&quot;,&quot;<SCRIPT FOR='window' EVENT='onload'
LANGUAGE='JScript'>document.all.&quot; + control.UniqueID
+ &quot;.focus();</SCRIPT>&quot;);
}
}

}
}

In my application, I set a reference to this new dll, and import the class that i have this function housed in. Once I have that set up, I can put this line of code in my PageLoad sub in my .vb file:

Class.SetInitialFocus(txtUserID)
This will then set the focus to my textbox called txtUserID on startup.

Now I know what you're saying: Isn't it easier to just write the 5 lines of code over and over again?! Well, it depends: If you aren't going to be using Visual Studio.NET for your development, this probably won't help. But I'm finding that when I use VS.NET, its much easier and cleaner to just keep all my code on teh .vb file side of things, and not the html side. Also, the ability to create generic code that accepts a few parameters and dynamically creates javascript for ANY situation you could want is a very tempting thought.

Anyway, just wanted to share another way of looking at this &quot;set focus&quot; thing with you.
:)

Talk later,
Jack

 
Hi Jack, thanks for the post!!! I'd much rather add this function to my web control library than be updating HTML again and again. The more reusable controls I have the better. I am using VS.NET and C# for development and I realize that creating reusable controls is a major advantage for reliable rapid application development so your post was very valuable to me. Also, you are so right that creating generic code that dynamically creates javascript is very intriguing. It opens up a lot of new possibilities. Thanks again!

Kyle
 
Anybody know how I would do this with a VB class file? Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Okay I think I got the class part:


Public Class toolBox
Public Sub SetInitialFocus(ByVal control As Control)
If control Is &quot;&quot; Then
Throw New ArgumentException(&quot;The Control must be added to a Page before you can set the Initial Focus to it.&quot;)
End If

If control.Page.Request.Browser.JavaScript = True Then
control.Page.RegisterClientScriptBlock(&quot;InitialFocus&quot;, &quot;<SCRIPT FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all.&quot; + control.UniqueID + &quot;.focus();</SCRIPT>&quot;)
End If

End Sub

End Class


However, when I try to use it toolbox.SetInitialFocus(txtEmailAddress). The toolbox.SetInitalFocus(txtEmailAddress) turns blue and says &quot;'SetInitalFocus' is not member of forums.toolBox&quot;. Any thoughts on this?

Thanks

Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Hey Wayne,

Did you just try and convert it to VB.NET code and insert it into your file, or did you create it in a seperate dll?

If its in a seperate dll, you're referencing it properly right?

jack
 
Well I want to create it in a new file so I can just take that fine to a new project and use it.

I put it in an Add New Item > Class file which shows up as a [name].vb file. I assume it is going to compile it in a .dll itself or at least the stuff that I have been reading makes it sound like that. It will compile on the fly and put in a /bin directory? I show no physical evidence of this yet, but that is the way that sounds like it should work to me. Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Hi Wayne,

As refered to by Jack's question, you must register the dll in the project in order to use it. In the Solution Explorer under your project right click on References and select Add Reference. Browse to your dll and select it.

Kyle
 
Hey Wayne,

Here's what you need to do to create a dll:
1. Create a new project of type CodeLibrary
2. Add your .vb files, which are the classes for the dll
(sounds like you've already covered these two steps)
3. Either click the Play button, or right click your project in the project explorer and select Build. This will actually create teh compiled DLL for you.
4. Do Kyle's post about referencing your dll in your app

Now, a few things I've encountered with dll's:
-Make sure that your namespaces make sense, or you might find yourself typing crazy long names to access them
-Make sure you have your project path set so it makes sense. I compiled a dll and couldn't figure out where it was until I realized I had created my project folder as a subfolder to another project! VS is not very good with changing those either (I find anyway)

If you need any other info, let us know
:)

Jack
 
Good grief Charlie Brown. First things first, I could not find anything called a Code Library, I did find, however; Class Library, Windows Control Library, and Web Control Library and I used Class Library.

Now I copied the 'Public Sub SetInitalFocus(ByVal control As Control)' Sub into the class. However, the ...As Control turns blue and says &quot;Type 'Control' is not defined&quot;. It was &quot;working&quot; before why is not working now?

Any ideas? What should I change it too?


Thanks


Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Of all the Wayne Sellars of the world, your the Wayne Sellarsest (heh heh, sorry, couldn't resist the Charlie Brown quote)
;)

Anyways, sorry, my bad: it is class library, you're right. The reason that the control is coming up blue is because you probably havn't added System.Web as a reference in the project. Try that and it should fix it.

Jack
 
I made a reference to System.Web and did not work also along those lines I made a reference to the following and none of these worked either.

system (default)
system.Data (default)
system.configuration.install
system.design
system.directoryServices
system.drawing
system.drawing.design
system.enterpriseServices
system.management
system.messaging
system.runtime.remoting
system.runtime.serialzation.formatters.soap
system.security
system.servicesprocess
system.web
system.web.mobile
system.web.regularexpressions
system.web.services
system.windows.forms
system.xml (default)
Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Okay, what I had to do to get to control to &quot;work&quot; is I had to at a reference to System.Web but I had to do an &quot;Imports System.Web.UI&quot; at the top and it was happy, of know.

W Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
This web page seems to be a good reference:

Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Okay, another question concerning this:

When I get the point I am added the util.dll that I created earlier, then I added the &quot;Imports util&quot; line to the top.

Which so far so good.

But when I add the

Toolbox.SetInitialFocus(txtEmailAddress)

I get a blue squiggly underneath Toolbox.SetInitialFocus, also keep in mind that when I type toolbox it is found by the system; however, when I add the dot, ReferenceEquals and Equals are the only options that are availble. There should be an option for SetInitalFocus, but it is not there.

This is the contents of the class which has a the Namespace setup as Util. It also References: System; System.Data; System.Web; System.XML.


//--------------------------------------(snip)

Imports System.Web.UI

Public Class Toolbox

Public Sub SetInitialFocus(ByVal control As Control)
If control Is &quot;&quot; Then
Throw New ArgumentException(&quot;The Control must be added to a Page before you can set the Initial Focus to it.&quot;)
End If

If control.Page.Request.Browser.JavaScript = True Then
control.Page.RegisterClientScriptBlock(&quot;InitialFocus&quot;, &quot;<SCRIPT FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all.&quot; + control.UniqueID + &quot;.focus();</SCRIPT>&quot;)
End If
End Sub

End Class

//--------------------------------------(snip)

Any ideas on what is wrong with this?

Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Hi Wayne,

>>>
When I get the point I am added the util.dll that I created earlier, then I added the &quot;Imports util&quot; line to the top.
<<<

It is not necessary to add &quot;Imports util&quot; to the top of the code page. It may even be causing your problem, I don't know that for sure. Just add the reference under References in the Solution Explorer.

>>>
But when I add the
Toolbox.SetInitialFocus(txtEmailAddress)
I get a blue squiggly underneath Toolbox.SetInitialFocus,
<<<

I think the reason you may not be getting the results you expect is because you need to reference the namespace, then the class and then the function. From what you have written so far I would guess the to be:

Util.Toolbox.SetInitialFocus(txtEmailAddress)

>>>
also keep in mind that when I type toolbox it is found by the system; however, when I add the dot, ReferenceEquals and Equals are the only options that are availble. There should be an option for SetInitalFocus, but it is not there.
<<<

I'm not sure why this is happing, it may have to do with the &quot;Imports util&quot; line that was added to the top or you have some other class named Toolbox on your code page. If I type Utils. I then get Toolbox and then I get SetInitialFocus. When I type Toolbox without the namespace first I don't get the Intellisense selection list because there is no reference to a Toolbox class in the namespace on the my code page. That is why I am referencing the function with the namespace.class.function that is in the dll.

>>>
This is the contents of the class which has a the Namespace setup as Util. It also References: System; System.Data; System.Web; System.XML.
<<<

I have &quot;Imports System&quot; and &quot;Imports System.Web.UI&quot; at the top of the code page and System, System.Drawing and System.Web listed under References. Actually for me it's &quot;using System&quot; and &quot;using System.Web.UI&quot; because I'm writing in C#. Wayne or Jack, do either of you know the difference between the &quot;Imports&quot; at the top of the page and the References in the Solution Explorer?

It just came to mind that you may have your code all under the same namespace and then again maybe not. I don't so you will have to take that into consideration concerning this discussion. I have one namespace for the application and another for the SetInitialFocus control. We may still be missing information that would help to clear up this problem for you. If the above discussion doesn't do it then maybe posting the entire code page from your application and the SetInitialFocus control will help Jack and I nail down the problem for you. Also it's important to note that I am a C# programmer and I am having to interpret the VB.NET stuff. There could be differences in the language like C# is case sensitive and I don't know if VB.NET is case sensitive or not. By the way, is VB.NET case sensitive?

Let us know how it's going.

Kyle
 
My application is one namespace and my utilties is another so I can reuse them without trouble in future projects. I have tryed it both ways util.toolbox. and toolbox. but neither will give me the SetInitialFocus method as an option. And with and without the Imports statement.

I do not know what the diffence between References and Imports, I have that on list to find out but I can find any enough completed documentation on the two to compare them. However, I do know that References do not have all the options availiable to you for example if you right click add you will notice that there is no System.Web.UI namespace; however, on the Microsofty web site it is listed as availiable option and I can add it manually and the system knows where to find it but it does not show up in the Add Reference dialog. I will be interested in seeing the answer to that one.

I have added this project to a downloadable area:


I have been playing with register.aspx

I appreciate any help I can get on this, I am reading everything I can in order to get this figured out but the documentation to really bad right now.

BTW, I can tell if VB.NET is case sensitive or not. Regular VB is not, but the VS.NET design environment changes the cases of everything to case it which it was orginial declared or initialized.

Thanks


Wayne Sellars

&quot;Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0.&quot;
 
Wayne,

I started to run your project only to realize I do not have VB.NET installed, only C#. At the time I installed it I was forcing myself to only use C#. I'm on my laptop at home and the VS.NET CD is at work. The frist opportunity I have I will install the VB.NET options and then take a look.

Another question, what version of VS.NET are you running, Beta 2 or Release Candidate 1?

Kyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top