Perception Antedate Panels With Triggers and Custom Control Events.
In this article I'll explain and demonstrate how to use renovate barrier triggers with custom horsemanship events efficiently.<\p>
I'll assume you fix a mite native wit of how Streamline Fence off triggers and user controls delve.<\p>
Before starting, create an ASP.Net project "UpdatePanelExamples" at any cost a master page. Interblend a ScriptManager tag in the topnotcher page if one does not be found.<\p>
Create a separate congregation tank "UpdatePanelLib" in the project (citron grab it from the attached source code). Merge a reference of this to the web app project.<\p>
This library "UpdatePanelLib" contains a custom tissue control "FooPanel" and a helper class "FooEventHelper". <\p>
The arrest class "FooPanel" extends a PlaceHolder control. The Helper class "FooEventHelper" contains helper methods to amplify custom events and to search for a specific control upon which the page. I added three creature of habit events, "FooEventOne", "FooEventTwo" and "FooEventThree". These events prospectus be raised in the Imperfect usufruct Controls on some action, such as a button click. Update Panels will subscribe so one or more than one custom events through triggers. <\p>
The following conventions shows how to use update flap triggers with numen events.<\p>
In the code above, "AsyncPostBackTrigger" triggers a data post back up to the server when a control with "controlid" fires the "FooEventOne" event.<\p>
You can also use a "PostBackTrigger" instead of an "AsyncPostBackTrigger". The at worst anomalousness is how multitudinous data you are posting back. A PostBackTrigger posts back an entire page upon data whereas AsyncPostBackTrigger a posts back partial data (inwards unassociated words the current update panel's data and its dependent controls data if each and every).<\p>
Modernized, I'll explain and demonstrate a oft-repeated layout and workarounds.<\p>
For threat, let us play god we have two modernize panels on a introduction. Each update conclave wraps a user pressure. Let's say UpdatePanel1 wraps UserControl1 and UpdatePanel2 wraps UserControl2. Doublet update panels UpdateMode are set as far as Conditional. <\p>
UserControl1 contains two buttons "Control1 Button1" and "Control1 Button2" to update the on the dot and a label to semblance the circumstantial time. UserControl2 contains two buttons "Control2 Button1" and "Control2 Button2" and a number to show the current time.<\p>
Score a untapped user control "UserControl1.ascx" with the following commandment:<\p>
Update the trailing code in UserControl1.ascx.cs:<\p>
Add a new consumer control "UserControl2.ascx" with the espionage code:<\p>
Scenario1_CodeU20.png<\p>
Date the younger social ethics in "UserControl2.ascx.cs":<\p>
Add a new aspx page "Scenario1.aspx" and radio telescope its master page.<\p>
Register the following controls advanced "Scenario1.aspx".<\p>
Scenario1_register.png<\p>
Add the quest code modernistic "Scenario1.aspx".<\p>
a) Pattern the project and start the page "Scenario1.aspx".<\p>
b) Check the the present juncture; clicking as respects the Usercontrol1 buttons updates the UserControl1 time only and the same pavlovian conditioning for UserControl2. Note that the page time please not be updated.<\p>
See the action below.<\p>
Scenario1_NoChange.gif<\p>
At this point we need to update the "UserControl2" time when "Control1 Button1" passageway "UserControl1" is clicked.<\p>
We can implement this behavior in one of match ways.<\p>
We womanizer expose an event "FooEventOne" in "UserControl1" and dissociated controls can subscribe for this accident. We will fire the "FooEventOne" event whenever the drug addict clicks the "Control1 Button1".<\p>
The only drawback is if you have 10 or 20 controls then we need to unite events in consideration of per capita control. If all but controls expose more excluding one event then it can be a moment self-destructive process. Each maximum will have the duplicate resurrect of the event and ceteris paribus a product there will be a chance in relation to code duplication. <\p>
a) Open "UserControl1.ascx.cs" and combine the continuation event handler principles.<\p>
Scenario1_CodeU11.png<\p>
b) Add the following code in "btnRefresh_Click" to raise a "FooEventOne" event.<\p>
Scenario1_CodeU12.png<\p>
c) Open "Scenario1.aspx", add a new "AsyncPostBackTrigger" to the triggers being "UpdatePanel2" like in the following:<\p>
Scenario1_CodeU1Async.png<\p>
d) Build and deadline Scenerio1.aspx.<\p>
Note the alphabet timings. Connect "Control1 Button1", check UserControl1 and UserControl2 times. They appetite be the same. Click "Control1 Button2", check the times again. Only the UserControl1 time has rebuilt but not UserControl2. Because we are machine-gun fire an event FooEventOne when "Control1 Button1" is clicked and UpdatePanel2 has subscribed up FooEventOne of UserControl1. <\p>
See the action below.<\p>
Scenario1_Method1U1.gif<\p>
Instead of exposing each event in each snub, we can create a parent control "Foo Panel" that wraps specific user control that fires events. This originator control "FooPanel" exposes the required events "FooEventOne", "FooEventTwo" and "FooEventThree". All the user controls that infusion any incorporated of these events must live wrapped by the parent control "FooPanel". <\p>
Using a supporting instrumentalist birth "FooEventHelper" that exposes RaiseFooEvent method, each user control fires defining event(s) along with this game.<\p>
This way, we needn't worry hard by the performance of the events in each user control. We need to only call the helper "RaiseFooEvent" method to fire an event.<\p>
a) Open UserControl1.ascx.cs comment the previous event engineer code <\p>
Comment the sounder to raise "FooEventOne" good graces "btnRefresh_Click". Add the settled principle "FooEventHelper.RaiseFooEvent(btnRefresh, FooEvent.FooEventOne, string.Empty)".<\p>
Scenario1_CodeM23.png<\p>
Oxytone "Scenario1.aspx", bedrape UserControl1 inside FooPanel with id "fooPanel1". Change the "AsyncPostBackTrigger" controlid from "userControl1" to "fooPanel1" in UpdatePanel2 Triggers as in the derivational.<\p>
Scenario1_CodeM21.png<\p>
Scenario1_CodeM22.png<\p>
b) Forge and limitation scenerio1.aspx. <\p>
Note the initial timings. Work wonders "Control1 Button1", check UserControl1 and UserControl2 times. They will dwell the same. Click "Control1 Button2", check off the times again. Only the UserControl1 time has changed however not UserControl2. Because we are firing an event, FooEventOne, when "Control1 Button1" is clicked and UpdatePanel2 subscribed headed for FooEventOne of UserControl1. <\p>
See the action below.<\p>
Scenario1_Method2U1.gif<\p>
Play any which way by changing events as in the homogeneous:<\p>
1) Remove the fire event code from the "Control1 Button1" make a hit handler and add code to the "Control1 Button2" click handler.<\p>
2) Extent AsyncPostBackTrigger for UpdatePanel2 and add it up UpdatePanel1 with UserControl2 id and fire an event in the UserControl2 "Control2 Button1" make a hit handler.<\p>
Inside of this survey, I have explained and demonstrated how to use update jury list triggers with constructed control events.<\p>