Can I set initial focus from server-side into textbox?

K

Kent P. Iler

Hi,

I am showing/hiding a variety of panels that have different text boxes via
code-behind functions. Depending on the condition, I would like to have the
focus be set on a specific text box. However, all examples I've seen do
this from the client side.

Does anyone have a way to easily to specify the textbox to focus on from a
code-behind function? My guess is it will have to be some combination of
server-side and client-side coding.

Any help would be appreciated.

Thanks!

-- Kent Iler
 
C

Chezhian

HI Kent P. Iler ,

The followint function will helps you to set the focus to the
specified control through the Code behind.

Only the thig you have to pass the Control name as string Parameter

ex : SetAlertFocusControl("txtName")

Not you can place the focus to all the Control With this generic function.

Public Sub SetAlertFocusControl(ByVal ControlName As String)
Dim strScript As String ' for storing the script generated
Try
strScript = "<script language=" + Chr(34) + "javascript" + Chr(34) + ">"
strScript &= " var control = document.getElementById(" + Chr(34)
strScript &= ControlName + Chr(34) + ");"
strScript &= " control.focus();"
strScript &= "</script>"
' register the generated script
Page.RegisterStartupScript("Focus", strScript)
Catch excp As Exception
Finally
End Try
End Sub

Hope this helps ! all the best
 
G

Guest

Hi Kent,

Yeah you can bring to the focus to some controls based on some condition. I
believe you know how to bring the focus to a control using java script. What
you need to do here is make that script to trigger from the code behind file.

There are two ways to running the script from the code behind file.

1. Printing the script using response.write.

eg. Response.Write("<script>text1.focus()</script>")

2. Using Page.RegisterStartupScript

Page.RegisterStartupScript("foc", "text1.focus()")

Page.RegisterStartupScript will execute the script.

With regards,

Sadha Sivam S,
Microsoft Community Star,
Sr. Software Engineer,
Aspire Systems,
www.dotnetspider.com
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top