Attention: We are retiring the ASP.NET Community Blogs. Learn more >

A silly matter of style

A long time ago (in blog time) Fabrice pointed out this nice tip from ScottGu:

Private 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('" & scriptClientId & "').focus();")
    scriptFunction.Append("</script>")

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

End Sub

I think it looks nicer like this:

private void setFocus(Control controlToFocus)
{
    string scriptCode = string.Format(
        "<script language='javascript'>"
        + "document.getElementById('{0}').focus();"
        + "</script>",
        controlToFocus.ClientID );

    RegisterStartupScript("focus", scriptCode);
}

Of course, I *don't* mean the C# syntax vs. the VB.NET syntax (both languages are as powerful, which one looks better is a personal preference, please don't flame me) but to the use of String.Format() as opposed to StringBuilder. May be (just may be) StringBuilder is marginally faster, but the String.Format() code is definitively shorter and (just) probably easier to understand. Opinions?

3 Comments

  • Expect to get flamed about not using the StringBuilder. I think it's six in one hand, half dozen in the other. I think Scott's way is a tad easier to understand (although I personally would remove the scriptClientID variable because it is not necessarily necessary.

  • I think in the case of this example, it's fine not to use a StringBuilder, there probably wouldn't be much difference, but it's definitely &quot;faster&quot; than regular string concatination





    When shortening code, you really have to weigh what you're doing vs. the gained results...Is the code now easier to read? Or is it just shorter and actually harder for some joe schmoe to come sit down, look at and understand now? Sometimes shorter is better, sometimes not.

  • This was a great piece of code that really helped me to solve this annoying problem that I had that every time that a postback would take place, the form would start up at the beginning having to have the need to scroll all the way down to the control again. I only made some minor modifications and it work great!



    Thanks you very much!





    sub myOnTextChangedHandler()

    call SetFocus(txtMyTextBox)

    end sub



    Private Sub SetFocus(ByVal controlToFocus As Control)

    Dim scriptFunction As New StringBuilder

    Dim scriptClientId As String

    scriptClientId = controlToFocus.ClientID

    scriptFunction.Append(&quot;&lt;script language='javascript'&gt;&quot;)

    scriptFunction.Append(&quot;document.getElementById('&quot; &amp; scriptClientId &amp; &quot;').focus();&quot;)

    scriptFunction.Append(&quot;&lt;&quot;)

    scriptFunction.Append(&quot;/&quot;)

    scriptFunction.Append(&quot;script&gt;&quot;)

    RegisterStartupScript(&quot;focus&quot;, scriptFunction.ToString())

    End Sub

Comments have been disabled for this content.