|
-
Apr 17th, 2003, 09:01 AM
#1
Thread Starter
Junior Member
newB: set focus textbox w/WebControls?
Hello all this is my first post and I am new to VB and .NET.
I would like to set focus to a textbox when my page loads.
Here is some of what I have:
Protected WithEvents textbox1 As System.Web.UI.WebControls.TextBox
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set position of cursor in text box.
'textbox1.focus 'NOT WORKING??
I think the problem is with WebControls. Is there another option?
I don't know about WebControls vs Others. I am trying to modify existing code.
I have also tried to set the tab order but this did not help.
Thanks.
-
Apr 17th, 2003, 09:40 AM
#2
Lively Member
Do you specifically need to use a web control?
System.Web.UI.WebControls.TextBox does not appear to have an entry for Focus in the help, whereas System.Windows.Forms.TextBox does.
-
Apr 17th, 2003, 09:54 AM
#3
PowerPoster
You might want to check out googles home page source code. It has some javascript that I think does this.
-
Apr 17th, 2003, 02:06 PM
#4
Thread Starter
Junior Member
I found this in the ASP .NET forum written by Dr.Who/Tim, it works.
This will work, except on self-made usercontrols...
just add a 'imports system.text' line before your class dec.
add this to your startup:
Dim strBuilder As StringBuilder = New StringBuilder()
strBuilder.Append("<script language='javascript'>")
strBuilder.Append("function setFocus() {")
strBuilder.Append("document.getElementById('TextBox3').focus();}")
strBuilder.Append(" window.onload=setFocus;")
strBuilder.Append("</script>")
RegisterClientScriptBlock("Focus", strBuilder.ToString)
Remember to change 'TextBox3' to yourTextBoxId
If anyone has a better way please post. But this does work!
Thank you Dr.Who
-
Jun 14th, 2004, 09:48 AM
#5
This can't be the only way can it?
If it is then this is VERy silly 
Woka
-
Jun 14th, 2004, 10:06 AM
#6
Frenzied Member
Keep in mind server side controls can't handle a lot of the logic that you are accustomed to using in VB. It doesn't make sense.
You need to use client-side scripts to do this.
Remember, the asp.net server sends html code for the browser to interpret so anything that isn't possible in HTML has to be done with a script.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|