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

No related Blog found.

No related videos found.

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

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Healthgrades Logo

Healthgrades

NIT A/S

Schlumberger Logo

Schlumberger

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Trayport Logo

Trayport

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.

Ericson Logo

Ericson

ProgramUtvikling Logo

ProgramUtvikling

Jack Links Logo

Jack Links

Freadom Logo

Freadom

Microsoft Logo

Microsoft

Sage Logo

Sage

ALS Life Sciences Logo

ALS Life Sciences

Boxit Document Solutions Logo

Boxit Document Solutions

Akaditi Logo

Akaditi

Genus Breeding Ltd Logo

Genus Breeding Ltd

Workday Logo

Workday

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

Royal Air Force Logo

Royal Air Force

Nottingham County Council Logo

Nottingham County Council

Philips Logo

Philips

SuperControl Logo

SuperControl

Genus Breeding Ltd Logo

Genus Breeding Ltd

Lean SA Logo

Lean SA

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation