473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting Focus to a Control - Web

Hello All,

Does anyone know how to set the focus to a textbox control on Page_Load?

Thank you,
Glenn
Nov 19 '05 #1
5 1345
Glenn u can achieve this using JavaScript!
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #2
Or use this:-
<html>
<head>
<script language="JavaScript">
function Focus()
{
formID.urname.focus();
}
</script>
</head>
<body onload="Focus();">
<form id="formID">
Name: <input type="text" id="urname" /><br />
Age: <input type="text" id="membership" />
</form>
</body>
</html>

Hope it helps
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #3
Thanks Patrick,

I tried implementing in to the HTML of the webForm the code you specified,
but didn't do something right and caused me to have to re-program the whole
page lol. My fault, I don't know HTML very well and was not able to get the
code back to what it was originally.

I'll try it again after making a copy of the next WebForm I program in case
I mess it up again.

Thanks for you input,
glenn


"Patrick Olurotimi Ige" <na********@hotmail.com> wrote in message
news:#b**************@TK2MSFTNGP14.phx.gbl...
Or use this:-
<html>
<head>
<script language="JavaScript">
function Focus()
{
formID.urname.focus();
}
</script>
</head>
<body onload="Focus();">
<form id="formID">
Name: <input type="text" id="urname" /><br />
Age: <input type="text" id="membership" />
</form>
</body>
</html>

Hope it helps
Patrick

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #4
Glenn try posting ur code and where you would like to implement it.
Patrick
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #5
Try this:-

Public Shared Sub SetFocus(ByVal control As Control)
If control.Page Is Nothing Then
Throw New ArgumentException("The Control must be added to a Page
before you can set the IntialFocus to it.")
End If
'If the control is enabled and visible select it
If control.Visible And control.EnableViewState Then
If control.Page.Request.Browser.JavaScript = True Then
' Create JavaScript
Dim s As StringBuilder = New StringBuilder
s.Append(vbCrLf + "<SCRIPT LANGUAGE='JavaScript'>" + vbCrLf)
s.Append("<!--" + vbCrLf)
s.Append("function SetInitialFocus()" + vbCrLf)
s.Append("{" + vbCrLf)
s.Append(" try " & vbCrLf)
s.Append(" {" & vbCrLf)
s.Append(" document.")
' Find the Form
Dim p As control = control.Parent
While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
p = p.Parent
End While
s.Append(p.ClientID)
s.Append("['")
s.Append(control.UniqueID)
' Set Focus on the selected item of a RadioButtonList
If TypeOf control Is RadioButtonList Then
Dim rbl As RadioButtonList = control
If Not rbl Is Nothing Then
Dim suffix As String = "_0"
Dim t As Int32 = 0
Dim li As ListItem
For Each li In rbl.Items
If li.Selected Then
suffix = "_" + t.ToString()
Exit For
End If
t = t + 1
Next
s.Append(suffix)
End If
End If
' Set Focus on the first item of a CheckBoxList
If TypeOf control Is CheckBoxList Then
s.Append("_0")
End If
s.Append("'].focus();" + vbCrLf)
s.Append(" }catch(e){}" & vbCrLf) ' close the try, catch
block
s.Append("}" + vbCrLf)
If control.Page.SmartNavigation Then
s.Append("window.setTimeout(SetInitialFocus, 500);" +
vbCrLf)
Else
s.Append("window.onload = SetInitialFocus;" + vbCrLf)
End If
s.Append("// -->" + vbCrLf)
s.Append("</SCRIPT>" + vbCrLf)
' Register Client Script
control.Page.RegisterClientScriptBlock("InitialFoc us",
s.ToString())
End If
End If
End Sub

Regards

Toby.

"Glenn T. Kitchen" <Ki******@Ameritech.net> wrote in message
news:Op*************@TK2MSFTNGP15.phx.gbl...
Hello All,

Does anyone know how to set the focus to a textbox control on Page_Load?

Thank you,
Glenn

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IMRAN SAROIA | last post by:
Hi! Please advise how to set focus to any control on Web and Windows Forms. Regards Imran
2
by: MLH | last post by:
A form named frmVehicleEntryForm has a number of textbox controls who's OnGotFocus property setting is an expression... =Change2Green() Change2Green() looks something like this... Dim MyControl...
12
by: CLEAR-RCIC | last post by:
Hi, I'm having problems setting focus to a textbox on a web user contol on an asp.net web page. The following script works on normal asp.net pages: <script language="javascript"> function...
2
by: Sridhar | last post by:
Hi, I have a question. I have created a user control which contains a textbox and a button. we will enter some search word in the textbox and hit the button. Then we will get a pop-up with the...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for...
7
by: Zytan | last post by:
I want to set a control in a form to have the default focus. This is not the accept button -- that's set to a button when is 'clicked' when you press ENTER. I want a textbox to have the focus...
5
by: Finn Stampe Mikkelsen | last post by:
Hi How can i set a focus to a textbox in my codebehind page?? I have this WebForm, that takes information from a user and 2 buttons on the form. One that takes action on the entered...
3
by: kelvin.koogan | last post by:
I have a number of controls on a tab page. I want to validate them all when the user tries to leave the tab. I then want to highlight the first control which fails validation. How can I do this? I...
2
by: angi35 | last post by:
I hope this is an easy question for someone out there. In Access 2000…I have a MainForm with a tab control (MAIN TABS) with 7 tabs. Within each tab is a SubForm. Within each SubForm is a tab...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.