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

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

TL;DR; 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
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

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.​

Microsoft Logo

Microsoft

Emerson Process Management Logo

Emerson Process Management

CR2

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

SuperControl Logo

SuperControl

Kongsberg Maritime Logo

Kongsberg Maritime

Philips Logo

Philips

Schlumberger Logo

Schlumberger

Lockheed Martin Logo

Lockheed Martin

NIT A/S

Boxit Document Solutions

Graham & Brown Logo

Graham & Brown

Healthgrades Logo

Healthgrades

Illumina Logo

Illumina

Qualco Logo

Qualco

Bistech Logo

Bistech

Royal Air Force Logo

Royal Air Force

Ghana Police Service Logo

Ghana Police Service

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Nottingham County Council Logo

Nottingham County Council

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Washington Department of Transport Logo

Washington Department of Transport

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Qualco Logo

Qualco

Deliotte Logo

Deliotte

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Philips Logo

Philips