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
No related videos found.
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.
We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.
Alignment Healthcare
DFDS
Teleplan
ALS Life Sciences
MacDonald Humfrey (Automation) Ltd.
Graham & Brown
Kongsberg Maritime
Epic Games
ProgramUtvikling
Ericson
Higher Education Statistics Agency
Boeing
Xceptor - Process and Data Automation
Big Data for Humans
Genus Breeding Ltd
Healthgrades
NIT A/S
Akaditi
Royal Air Force
Department of Work and Pensions (UK)
Washington Department of Enterprise Services
New Hampshire Supreme Court
Nottingham County Council
Washington Department of Transport
Genus Breeding Ltd
Microsoft
Epic Games
Emerson Process Management
DFDS
Xceptor - Process and Data Automation