ChildrenasTriggers in UpdatePanel


Recently I was reading about the UpdatePanel and I came across a property called ChildrenAsTriggers. As you all know UpdatePanels has 2 types of triggers

  1. AsyncPostbackTrigger
  2. PostBackTrigger

Both of these triggers cause a postback to server depending on the trigger type we use. But there is another property called ChildrenasTriggers. This property tells you whether child controls needs to be update when a post back happens. By default this property is set to True i.e always update the child controls. If you are wondering why we need to set this property to false , consider a scenario where we have update panel inside an update panel (Nested Update Panel) like the below.

<form id=”form1″ runat=”server”>

<div><asp:ScriptManager ID=”ScriptManager1″ runat=”server”></asp:ScriptManager

<asp:UpdatePanel ID=”upOuterPanel” runat=”server” ChildrenAsTriggers=”false” UpdateMode=”Conditional”><ContentTemplate>

<div>Outer Last Updated = <%=DateTime.Now.ToLongTimeString()%> </div>

<div><asp:Button ID=”btnOuter” runat=”server” Text=”Outer Update” /> </div>

<asp:UpdatePanel runat=”server”  ID=”upInnerPanel” >

<ContentTemplate<divInner Last Updated = <%=DateTime.Now.ToLongTimeString()%></div

<div<asp:Button ID=”btnInnerUpdate” runat=”server” Text=”Inner Update” />

</div>

</ContentTemplate>

</asp:UpdatePanel

</ContentTemplate>

</asp:UpdatePanel>

</div>

</form>

In the above code I have a button control in each of the update panels and notice the ChildrenasTriggers set to false for outer update panel. So this means when we click the Outer Update button the text for Outer Last Updated field is not updated. Also ChildrenasTriggers can be set to False only when the UpdateMode is set to Conditional. Attempting to do so without this condition results in an InvalidOperationException being thrown by the ScriptManager during the page’s PreRender event. Now if we click any button ( Inner or Outer ) the text for Outer Label will not change because we are telling the ScriptManager not to update the contents of the Outer Text whenever any async post back happens. But the inner label time always changes with any button click because upInnerPanel is a child control of upOuterPanel. If we set UpdateMode=”Conditional”
for upInnerPanel then if we click btnInnerUpdate then only upInnerPanel will be updated. Hope this helps..

Advertisements
This entry was posted in .NET Framework. Bookmark the permalink.

One Response to ChildrenasTriggers in UpdatePanel

  1. MohanSivaMaruthi Vajapeyajula says:

    Good explanation with the example. When you get a chance , please look at a real time scenario where we can use ChildrenasTriggers property. Typically these triggers are useful in scenarios where we need to update data in the control asynchronously may be at a time interval (Eg: Change the news in a control for every 1 minute). Please think of such scenario and bring out the realistic scenario , it will be helpful and we can use in future 🙂

    Maruthi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s