RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 127, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


Set Focus Script

  

''' -----------------------------------------------------------------------------

    ''' <summary>

    '''    Sets Focus on a control on the page.  Similar to something seen on a Windows Form.

    ''' </summary>

    ''' <param name="webControl">WebControl to focus on.</param>

    ''' <param name="htmlControl">HTML Control to focus on.</param>

    ''' <remarks>

    ''' </remarks>

    ''' <history>

    '''     [robz]      8/31/2004   Created

    ''' </history>

    ''' -----------------------------------------------------------------------------

    Protected Sub SetFocus(Optional ByVal webControl As System.Web.UI.Control = Nothing, Optional ByVal htmlControl As System.Web.UI.HtmlControls.HtmlControl = Nothing)

        Dim setCursor As New System.Text.StringBuilder

        Dim removeScript As Boolean = False

        Dim controlname As String

        'check to see if both are sent

        If Not (webControl Is Nothing) And Not (htmlControl Is Nothing) Then

            Dim ex As New Exception("You may only send one control, web or html. Not both.")

            Throw ex

        ElseIf Not (webControl Is Nothing) Then 'if it is a webcontrol

            controlname = webControl.ClientID.ToString

        ElseIf Not (htmlControl Is Nothing) Then 'if it is a htmlcontrol

            controlname = htmlControl.ClientID.ToString

        Else 'nothing has been passed and the script should be removed.

            removeScript = True

        End If

        'check to see if the script should be removed.

        If Not (removeScript = True) Then

            'create the javascript

            setCursor.Append("<script language='javascript'>")

            setCursor.Append("function setCursor() {")

            setCursor.Append("    if (document.getElementById('" & controlname & "') != null) {")

            setCursor.Append("          document.getElementById('" & controlname & "').focus();")

            setCursor.Append("    }")

            setCursor.Append("}")

            setCursor.Append(" setCursor();")

            setCursor.Append("</script>")

            'register the script

            RegisterStartupScript("setCursor", setCursor.ToString())

        Else

            'register over the top of the script with nothing

            RegisterStartupScript("setCursor", "")

        End If

    End Sub

posted on Thursday, October 07, 2004 9:46 PM

Feedback

# Custom Web Control Of the DataGrid - very enhanced

10/7/2004 9:51 PM | RobLog

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: