By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
----------------------------------------------------------------------
ke**@iler.NOSPAM.com
Please CC: all replies via e-mail
Remove .NOSPAM from e-mail address
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #2

P: n/a
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
"Kent P. Iler" wrote:
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
----------------------------------------------------------------------
ke**@iler.NOSPAM.com
Please CC: all replies via e-mail
Remove .NOSPAM from e-mail address

Nov 19 '05 #3

P: n/a
Something basic like this should work:

Response.Write "<script language = javascript>"& MyTextBox.ClientID &
".focus()</script>"

Here is an even slicker approach:
http://SteveOrr.net/faq/2in1.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Kent P. Iler" <ke**@iler.NOSPAM.com> wrote in message
news:Nr********************@comcast.com...
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
----------------------------------------------------------------------
ke**@iler.NOSPAM.com
Please CC: all replies via e-mail
Remove .NOSPAM from e-mail address

Nov 19 '05 #4

P: n/a
Hi Kent,

You can make use form Data Format string property of the grid columns to
format the date.

For more input Refer http://www.dotnetspider.com/technology/kbpages/384.aspx.

With regards,

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

"Kent P. Iler" wrote:
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
----------------------------------------------------------------------
ke**@iler.NOSPAM.com
Please CC: all replies via e-mail
Remove .NOSPAM from e-mail address

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.