Utlizing DefaultButton and DefaultFocus in ASP.NET 2.0

Today I started my morning reviewing blogs and news sites and saw a link on www.asp.net for handling the enter key on Web Forms.  This has been an ongoing issue for me over the years.  I have tried some JavaScript solutions, just like everyone else, but at some point they all seem to fail.  I know I had read about a new way to set the Initial Form Focus on an ASP.NET 2.0 page with the DefaultFocus property, but had not had time to investigate it.  But I did spend a little time investigating the use of the DefaultButton property of ASP.NET 2.0 Forms and Panes this morning and was surprised by the results.  I also decided to extend that to the DefaultFocus property too.

If you know anything about the way I build ASP.NET websites, I am pretty big on base classes that inherit from the .NET framework.  I have a base class I inherit in all of my Code Behind classes, ExBasePage.  It contains a wealth of useful, reusable properties and methods I seem to use on every site.  In many instances I will create classes specific to a project that will contain extra properties and methods that I will use on that site over and over.  An example would be ExSSLPage, which ensures a page is only rendered via SSL.  It seems like I am always finding new methods and properties to drop down into my base class, today I added two more, DefaultButton and DefaultFocus.

    ''' <summary>
    ''' Sets the default button that will be clicked when a user hits the Enter key.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Property DefaultButton() As String
        Get

            If Not IsNothing(Master.Page) Then
                Return Master.Page.Form.DefaultButton
            Else
                Return Page.Form.DefaultButton
            End If

        End Get
        Set(ByVal value As String)

            If Not IsNothing(Master.Page) Then
                Master.Page.Form.DefaultButton = value
            Else
                Page.Form.DefaultButton = value
            End If

        End Set
    End Property

    ''' <summary>
    ''' Sets the Default Control to put the intial focus.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Property DefaultFocus() As String
        Get

            If Not IsNothing(Master.Page) Then
                Return Master.Page.Form.DefaultFocus
            Else
                Return Page.Form.DefaultFocus
            End If

        End Get
        Set(ByVal value As String)

            If Not IsNothing(Master.Page) Then
                Master.Page.Form.DefaultFocus = value
            Else
                Page.Form.DefaultFocus = value
            End If

        End Set
    End Property

First let me explain a little about what is going on.  I have defined two properties with very similar structures, one sets the DefaultButton the other the DefaultFocus for the page.  Since updating my sites to ASP.NET 2.0 I have started using Master Pages quite a bit.  Sometimes I do not need a master page, but still use my ExBasePage based class as my pages base.  In the later case, the Master.Page will be nothing and instead we need to reference the Page object.  I have put a simple catch in place here to handle that case.

The next thing I came to realize is you cannot just pass the ID of the control as a string, you must pass the control's UniqueId.  I found this out through some trial and error.  So sucessfully using the properties would look something like this:

                DefaultButton = btnSubmit.UniqueID
                DefaultFocus = txtMyTextBox.UniqueID

Now for the DefaultButton property, you need to pass it a control that is derived from IButtonControl, like a Button, LinkButton or ImageButton.  Otherwise you will throw an exception. 

Another note, is the default button property may also be used on Panes.  Since upgrading to ASP.NET 2.0 I am not using panes and have replced them with the MultiView.  At this point I do not see access to the DefaultButton property on the View class, but who knows.  You can always catch the ActiveViewChanged event and set the DefaultButton there.

My initial tests were successful and I plan on implementing this on a production site later today!

 

Share This Article With Your Friends!