ASP.NET Hosting

Tips from Scott Guthrie himself

Wow.

This is the cleanest solution to one of ASP.NET most common problems, setting focus:

Scott Guthrie notes in his
Blackbelt WebForms presentation:


Sub SetFocus(ByVal controlToFocus As Control)

Dim scriptFunction As New StringBuilder
Dim scriptClientId As String

scriptClientId = controlToFocus.ClientID

scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(scriptClientId)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")

RegisterStartupScript("focus", scriptFunction.ToString())

End Sub
[Richard Caetano]

The sample is from Scott's new set of slides here.

Richard beat me to it :), Very clean.

[ScottW]

A must read. See ScottGu's post.
The goodness even starts on the first slide: the ~ feature is one of those were you get a shock asking you "how come i did not know this?!".

3 Comments

  • The shock is that it's not plastered in every ASP.Net demo page on MSDN. Simple things like this are great, but only if you know about them. '~' should have been somewhere prominent in the ASP.Net docs.

  • Ok, but when I try to do it using Atlas.NETit doesn't work. I'm using ASP.Net 2.0 and Atlas, but look, just doesn't work with IE, if you use Firefox the native function Focus() works very well.

    Can you help me?? - I thing a lot of people have the same issue.

    Thanks a lot,

    José
    São Paulo / Brazil
    jb.alessandro@terra.com.br

  • .setfocus() solution

    Scriptmanager1.setfocus(objName)

    That's the solution

    José
    São Paulo / Brazil
    jb.alessandro@terra.com.br

Comments have been disabled for this content.