This one took be a bit of time to figure out, and really can be extremely useful in some cases. This is the situation, you want to set an unknown amount of controls as a triggers to your updatepanel. The problem is you typically have to do this the following way…
Dim mytrigger as new AsyncPostBackTrigger Mytrigger.ContorlID = “tab1”
Well this works if you have a set amount of triggers. You just do this each time for each trigger and it works out really well. But what if you don’t know how many controls there are. An example of when? Custom server controls, which I have been learning vigorously these last few months.
In this case we have a property which holds several button inputs, or tabs in my case. Well the problem is the developer can add multiple instances of these tab controls, so exactly how do we link them all to the updatepanel?
The answer is in the UpdatePanelTriggerCollection! This wonderful class is used to hold all the triggers within the updatepanel. So to do this we first create the updatepanel followed by the updatepaneltriggercollection.
Dim upnlContent as new updatepanel upnlContent.UpdateMode = UpdatePanelUpdateMode.Conditional Dim triggerCollectionContent as new updatepaneltriggercollection(upnlContent)
If you notice you must declare an owner to the triggercollection. In this case being upnlContent.
After you setup the triggerCollect you have to create your triggers with the following code:
triggerCollectionContent.Insert(0, New AsyncPostBackTrigger) CType(triggerCollectionContent.Item(0), AsyncPostBackTrigger).ControlID = "tab1"
The first line creates a new entry into the collection as a new trigger. The second line sets the controlID for the trigger. If you notice we didn’t have to assign it a variable name! Meaning it’s an array of triggers. So once you have finished inserting all of your triggers using a for loop you then go ahead and go back through that collection and add them to the updatepanel one by one as follows:
upnlContent.triggers.add(triggerCollectionContent.Item(0))
You use the same loop to add them to the updatepanel. Hope that helps you all out!
NOTE: When using this method with a for loop using control.controls.item(i), make sure to use an if statement to check the control type using control.controls.item(i).gettype is gettype(controltype)!!! And make sure to use i instead of 0 if you’re looping.
I am getting the following error, indicating that Item does not belong to UpdatePanelTriggerCollection. I have included the namespaces System.Web.UI and System.Collections.ObjectModel, but it still doesn’t work.
CS1061: ‘System.Web.UI.UpdatePanelTriggerCollection’ does not contain a definition for ‘item’ and no extension method ‘item’ accepting a first argument of type ‘System.Web.UI.UpdatePanelTriggerCollection’ could be found (are you missing a using directive or an assembly reference?)
Comment by Scott — June 25, 2009 @ 3:20 pm