467,075 Members | 1,089 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Give focus

Hello,

I found that (see below) to give the focus to a control(textbox) on an
asp.net page. There is no easiest way to do that ?

Thanks,

System.Text.StringBuilder sb = new System.Text.StringBuilder("");

sb.Append("<script language=\"JavaScript\">");
sb.Append(" document.forms[0].item(\"textbox1\").focus()");
sb.Append("</script>");

if (!IsStartupScriptRegistered("setFocus"))
{
RegisterStartupScript("setFocus", sb.ToString());
}
Nov 19 '05 #1
  • viewed: 1228
Share:
2 Replies
Not if you need to do it dynamically. You can hard code the script in the
..aspx of course, if it is something you always need.

"Christian Ista" <ml@cista.be> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Hello,

I found that (see below) to give the focus to a control(textbox) on an
asp.net page. There is no easiest way to do that ?

Thanks,

System.Text.StringBuilder sb = new System.Text.StringBuilder("");

sb.Append("<script language=\"JavaScript\">");
sb.Append(" document.forms[0].item(\"textbox1\").focus()");
sb.Append("</script>");

if (!IsStartupScriptRegistered("setFocus"))
{
RegisterStartupScript("setFocus", sb.ToString());
}

Nov 19 '05 #2
Here's some sample code that sets focus and kills the enter key:

"How can I disable enterkey in a form. "
================================================== ==================

Try using some javascript.
I added it to my Base page that I inherit from and inject it to each child
page.
(As a bonus you get to set the initial focus for any control)

================================================== ====================
Add code like this to any child page where you want to set focus to a
control or kill the enter key:

Public Sub New()
MyBase.New()

'set the focus to the UserID text box:
InitialFocus = txtUserId

'Do Not kill the Enter key on this page
Me.killEnterKey = False

'Kill the Enter key on this page
'Me.killEnterKey = True

End Sub

================================================== ====================
Add this code to your javascript library which is loaded for each page:

function NetscapeEventHandler_KeyDown(e) {
if (e.which == 13 && e.target.type != 'textarea' && e.target.type !=
'submit') { return false; }
return true;
}

function MicrosoftEventHandler_KeyDown() {
if (event.keyCode == 13 && event.srcElement.type != 'textarea' &&
event.srcElement.type != 'submit')
return false;
return true;
}

================================================== ====================
Add this to the top of your Base page:

Public InitialFocus As Control
Protected KillEnterKey As Boolean = True

================================================== ====================
Protected Overridable Sub Page_PreRender(ByVal sender As Object, ByVal e As
EventArgs)

Dim sb As New StringBuilder

'-- Focus
sb.Append("<script language='javascript'>")
If Not InitialFocus Is Nothing Then
sb.Append("document.getElementById('" & InitialFocus.ClientID &
"').focus();")
End If

'-- Append to the Browser Title set on an instance level

'Code to call the Enter button kill javascript in library
If KillEnterKey = True Then
sb.Append("var nav = window.Event ? true : false;")
sb.Append("if (nav) {")
sb.Append("window.captureEvents(Event.KEYDOWN);")
sb.Append("window.onkeydown = NetscapeEventHandler_KeyDown;")
sb.Append("} else {")
sb.Append("document.onkeydown = MicrosoftEventHandler_KeyDown;")
sb.Append("}")
End If

sb.Append("</script>")
RegisterStartupScript("My JScript", sb.ToString())

End Sub

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

--
Joe Fallon


"Christian Ista" <ml@cista.be> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Hello,

I found that (see below) to give the focus to a control(textbox) on an
asp.net page. There is no easiest way to do that ?

Thanks,

System.Text.StringBuilder sb = new System.Text.StringBuilder("");

sb.Append("<script language=\"JavaScript\">");
sb.Append(" document.forms[0].item(\"textbox1\").focus()");
sb.Append("</script>");

if (!IsStartupScriptRegistered("setFocus"))
{
RegisterStartupScript("setFocus", sb.ToString());
}

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by JM | last post: by
3 posts views Thread by Paul | last post: by
2 posts views Thread by Andy G | last post: by
1 post views Thread by psual | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.