Setting focus to FormView's child controls...

G

Guest

How do I set the focus to a control that is a child to a FormView? I've tried
the obvious (below) and lots of variations but none seam to work!

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
Me.FormView1.Row.FindControl("TextBox1").Focus()
End Sub
 
S

S. Justin Gengo

Dick,

Focus won't work on a web page. You have to set focus with javascript on the
web.

Here's a subroutine from a Javacript Component I give away for free on my
website:

Public Sub SetFocus(ByVal page As System.Web.UI.Page, ByVal webControl As
System.Web.UI.WebControls.WebControl)

If Not Page.ClientScript.IsStartupScriptRegistered("TextBoxFocus") Then

Dim StringBuilder As New System.Text.StringBuilder

With StringBuilder

..Append("<script language=""javascript"">" & vbCrLf)

..Append(vbTab & "<!--" & vbCrLf)

..Append(vbTab & vbTab & "document.getElementById('" & webControl.ClientID &
"').focus();" & vbCrLf)

..Append(vbTab & "//-->" & vbCrLf)

..Append("</script>" & vbCrLf)

End With

page.ClientScript.RegisterStartupScript(GetType(String), "TextBoxFocus",
StringBuilder.ToString)

End If

End Sub

This one is converted for .net 2.0 currently the component on the site is
written for .net 1.1 so if you need that you can see a demo of all the
scripts in the component and download the source code for free from here:

http://www.aboutfortunate.com/default.aspx?page=javascriptdemo

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
C

Christopher Reed

Try this:

Dim TextBox1 As TextBox = (TextBox)FormView1.FindControl("TextBox1")
TextBox1.Focus()
 
S

Steven Cheng[MSFT]

Hi Richard,

If you're going to reference any sub controls in the FormView, make sure
the ID is correct and the Template (where the control reside ) is the
current active template( e.g if the FormView is in edit mode, we can only
reference controls in the edit template...).

Also, for setting focus on a certain control, we can also use the ASP.NET
2.0 page class's "SetFocus" method(Page.SetFocus) to set the page's focus
on a certain control. We can pass the control's reference into the funciton.

Please have a try on it and let me know if there is still any problem.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome Richard,

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
B

Ben

Is there any way to do this for a textbox inside a datalist?
(So on Edit Template, the page should set focus on the textbox). Also if
the list spans more than the screen length, editing something below
should retain focus on the bottom and not reload page with the focus on
top.
Thanks in Advance,
Arthur
 
B

Ben

Is there any way to do this for a textbox inside a datalist?
(So on Edit Template, the page should set focus on the textbox). Also if
the list spans more than the screen length, editing something below
should retain focus on the bottom and not reload page with the focus on
top.
Thanks in Advance,
Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top