Understanding Update Panels With Triggers and Methodology Control Events.
Favorable regard this article I'll speed and raise a howl how to use update panel triggers with custom control events efficiently.<\p>
I'll change you have a smidgen knowledge in regard to how Update Panel triggers and user controls work.<\p>
Before starting, create an ASP.Takings project "UpdatePanelExamples" with a master page. Add a ScriptManager epilogue in the crowning attendant if one does not exist.<\p>
Create a separate class library "UpdatePanelLib" on the put forth (or enthrall them from the attached source code). Add a reference on this in the web app imagine.<\p>
This issue "UpdatePanelLib" contains a custom getup ordering "FooPanel" and a helper class "FooEventHelper". <\p>
The control desert "FooPanel" extends a PlaceHolder control. The Helper class "FooEventHelper" contains benefactor methods to raise decorum events and on route to pry seeing as how a specific control on the page. I added three tradition events, "FooEventOne", "FooEventTwo" and "FooEventThree". These events will be raised rapport the User Controls on some action, such for a button click. Modernize Panels will consent to up one motto more custom events wherewithal triggers. <\p>
The following code shows how in transit to objective update panel triggers with control events.<\p>
TriggerExample.png<\p>
In the code above, "AsyncPostBackTrigger" triggers a data magasin back till the server when a aplomb with "controlid" fires the "FooEventOne" event.<\p>
Alter can also use a "PostBackTrigger" instead of an "AsyncPostBackTrigger". The visibly difference is how maximal symptom you are recording back. A PostBackTrigger posts ignore an entire page regarding data limiting condition AsyncPostBackTrigger a posts reflex inclined data (inpouring other words the current update panel's data and its dependent controls data if any).<\p>
Cause, I'll explain and exhibit a hackney scenario and workarounds.<\p>
For example, let us assume we have two update panels on a chore boy. Each update panel wraps a user prescribe. Let's greeting UpdatePanel1 wraps UserControl1 and UpdatePanel2 seal of secrecy UserControl2. Both update panels UpdateMode are onrush to Conditional. <\p>
UserControl1 contains identical buttons "Control1 Button1" and "Control1 Button2" to update the time and a label in enactment the current sometimes. UserControl2 contains two buttons "Control2 Button1" and "Control2 Button2" and a label to follow from the base current waltz time.<\p>
Scenario1_UI.png<\p>
Add a of late cubehead control "UserControl1.ascx" with the following code:<\p>
Scenario1_CodeU0.png<\p>
Be dated the pursuit cryptoanalytics in UserControl1.ascx.cs:<\p>
Scenario1_CodeU1.png<\p>
Add a new user control "UserControl2.ascx" with the following code:<\p>
Scenario1_CodeU20.png<\p>
Update the following code in "UserControl2.ascx.cs":<\p>
Scenario1_CodeU2.png<\p>
Add a present-age aspx page "Scenario1.aspx" and set its master back matter.<\p>
Register the following controls in "Scenario1.aspx".<\p>
Scenario1_register.png<\p>
Ornament the following code in "Scenario1.aspx".<\p>
Scenario1_code1.png<\p>
Do the following:<\p>
a) Getup the project and start the page "Scenario1.aspx".<\p>
b) Check the present; clicking on the Usercontrol1 buttons updates the UserControl1 time only and the in any event behavior for UserControl2. Write out that the signature time will not obtain updated.<\p>
See the action beneath the sky.<\p>
Scenario1_NoChange.gif<\p>
Now we want to refurbish the "UserControl2" time when "Control1 Button1" avant-garde "UserControl1" is clicked.<\p>
We can implement this behavior entry one of two ways.<\p>
Method 1:<\p>
We burden expose an event "FooEventOne" in "UserControl1" and other controls can subscribe to this event. We will madden the "FooEventOne" event whensoever the user clicks the "Control1 Button1".<\p>
The only drawback is if you have 10 or 20 controls simultaneously we need towards add events to one and all slavery. If some controls expose some leaving out singular event then it cut it be a time consuming process. Each control will have the same copy of the exploit and as a result there will be a undertake speaking of code duplication. <\p>
Do the following.<\p>
a) Open "UserControl1.ascx.cs" and add the lateness event handler code.<\p>
Scenario1_CodeU11.png<\p>
b) Add the counterfeit cryptography in "btnRefresh_Click" to raise a "FooEventOne" event.<\p>
Scenario1_CodeU12.png<\p>
c) Open "Scenario1.aspx", agglutinate a new "AsyncPostBackTrigger" to the triggers for "UpdatePanel2" as in the espial:<\p>
Scenario1_CodeU1Async.png<\p>
d) Build and start Scenerio1.aspx.<\p>
<\p>
Note the initial timings. Click "Control1 Button1", check UserControl1 and UserControl2 times. They will come the very same. Click "Control1 Button2", check the times again. Only the UserControl1 time has changed but not UserControl2. Because we are direct fire an adventure FooEventOne yet "Control1 Button1" is clicked and UpdatePanel2 has subscribed to FooEventOne of UserControl1. <\p>
Feel the action below.<\p>
Scenario1_method1u1.gif<\p>
Method 2:<\p>
Instead as to exposing each event passage each control, we lockup procreate a parent control "Foo Bumper" that wraps specific user control that fires events. This stepmother control "FooPanel" exposes the vital events "FooEventOne", "FooEventTwo" and "FooEventThree". Nature the user controls that fire any one of these events must be wrapped consistent with the parent control "FooPanel". <\p>
Using a befriender class "FooEventHelper" that exposes RaiseFooEvent method, each user control fires specific conjuncture(s) with this method.<\p>
This way, we needn't worry about the execution of the events in each user control. We need so only call the helper "RaiseFooEvent" method to fire an outcome.<\p>
Do the following:<\p>
a) Open UserControl1.ascx.cs comment the previous affair handler code <\p>
Explanation the code to chase "FooEventOne" in "btnRefresh_Click". Add the code "FooEventHelper.RaiseFooEvent(btnRefresh, FooEvent.FooEventOne, string.Ho-hum)".<\p>
Scenario1_CodeM23.png<\p>
Freehearted "Scenario1.aspx", wrap UserControl1 inside FooPanel about id "fooPanel1". Change the "AsyncPostBackTrigger" controlid from "userControl1" to "fooPanel1" in UpdatePanel2 Triggers as in the following.<\p>
Scenario1_CodeM21.png<\p>
Scenario1_CodeM22.png<\p>
b) Build and start scenerio1.aspx. <\p>
Note the elemental timings. Rap "Control1 Button1", check UserControl1 and UserControl2 times. They definiteness remain the same. Click "Control1 Button2", ease up the concerns again. One the UserControl1 time has changed but not UserControl2. Because we are firing an event, FooEventOne, when "Control1 Button1" is clicked and UpdatePanel2 subscribed over against FooEventOne in reference to UserControl1. <\p>
Fade the action hellishly.<\p>
Scenario1_Method2U1.gif<\p>
Play around by changing events as in the following:<\p>
1) Remove the fire event code away from the "Control1 Button1" go over handler and adjoin code to the "Control1 Button2" click handler.<\p>
2) Remove AsyncPostBackTrigger from UpdatePanel2 and add it to UpdatePanel1 with UserControl2 id and fire an fact in the UserControl2 "Control2 Button1" click handler.<\p>
In this article, I have young explained and demonstrated how to use update open forum triggers with custom control events.<\p>



















