a·gen·tic a·gil·i·ty

Disable a timer at every level of your ASP.NET control hierarchy

Learn how to recursively find and disable all Timer controls in an ASP.NET page, even without knowing their IDs, to prevent unwanted UI updates and interference.

Published on
3 minute read
Image
https://nkdagility.com/resources/DPQr4iigMBP
Comments
Subscribe

Even though this sounds like a really simple thing, what if you do not know the name of the controls, and you do not want to have to add a bit of code that you, or another may developer may forget to every piece of code with a timer in it. The problem I have is that if you have a DropDownList on the same page as a update panel that updates based on a timer, you get a little interference.

If the user has the DropDownList open when the update occurs then the DropDownList closes. Very annoying.

The standard FindControl does not work as it requires an ID, so what if all you have is a type?

Well, you need a little extension method :)

 1Imports System.Runtime.CompilerServices
 2Imports System.Web
 3Imports System.Web.UI
 4
 5Module WebExtensions
 6
 7    <Extension()> _
 8    Friend Sub FindControls(Of T)(ByVal control As Control, ByVal list As List(Of T))
 9        If control.HasControls Then
10            Dim timers = control.Controls.OfType(Of T)()
11            list.AddRange(timers)
12            Dim subcontrols = From c In control.Controls Where Not timers.Cast(Of Control).Contains(c)
13            For Each c In subcontrols.Cast(Of Control)()
14                c.FindControls(Of T)(list)
15            Next
16        End If
17    End Sub
18
19    <Extension()> _
20    Friend Function FindControls(Of T)(ByVal control As Control) As List(Of T)
21        Dim l As New List(Of T)
22        control.FindControls(Of T)(l)
23        Return l
24    End Function
25
26End Module

I am pretty sure this is not the most efficient of code, and any recommendation on improving it are welcome…

Once this is in place it is easily called and actioned upon:

 1Sub OnPagePreRender(ByVal sender As Object, ByVal e As EventArgs)
 2
 3    ' Fix for pages with drop down lists
 4    FixForDropdownListsAndTimers()
 5End Sub
 6
 7Private Sub FixForDropdownListsAndTimers()
 8    ' Provcess timers
 9    Me.FindControls(Of System.Web.UI.Timer).ForEach(New Action(Of System.Web.UI.Timer)(AddressOf ProcessTimers))
10End Sub
11
12Private Sub ProcessTimers(ByVal t As System.Web.UI.Timer)
13    t.Enabled = Not DisableTimers
14End Sub

DisableTimers is set at the page level and filters down to the control so we can now disable all timers on a page when we want.

The FindControls method can find a list of all instances of a control type from an entire page, regardless of the nesting…

UPDATE: OK, so you have probably guessed that I am a complete muppet.. I have changes my UpdatePanels to UpdateMode=“Conditional” and with a few extra lines of code solved my problem the correct way! I will be keeping this little bit of code as you never know when you need to find all instances of a type of control :)… I am such a donkey…

Technorati Tags: .NET   CodeProject

Comments
Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Kongsberg Maritime Logo

Kongsberg Maritime

Teleplan Logo

Teleplan

Slaughter and May Logo

Slaughter and May

Philips Logo

Philips

SuperControl Logo

SuperControl

Hubtel Ghana Logo

Hubtel Ghana

Emerson Process Management Logo

Emerson Process Management

Alignment Healthcare Logo

Alignment Healthcare

Sage Logo

Sage

Illumina Logo

Illumina

YearUp.org Logo

YearUp.org

Epic Games Logo

Epic Games

Jack Links Logo

Jack Links

Qualco Logo

Qualco

Lockheed Martin Logo

Lockheed Martin

Genus Breeding Ltd Logo

Genus Breeding Ltd

New Signature Logo

New Signature

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

Ghana Police Service Logo

Ghana Police Service

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Royal Air Force Logo

Royal Air Force

Nottingham County Council Logo

Nottingham County Council

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Kongsberg Maritime Logo

Kongsberg Maritime

Slaughter and May Logo

Slaughter and May

SuperControl Logo

SuperControl

Boeing Logo

Boeing

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Sage Logo

Sage