Question

Simple Ajax program needs to update single update panel on a button click does both. Here is the updated code,

    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false"
            runat="server">
            <ContentTemplate>
                <fieldset style="width: 30%">
                    <legend>Panel - 1 </legend>
                    <asp:Label ID="label1" runat="server"></asp:Label>
                    <asp:Button ID="b1" runat="server" OnClick="both" Text="Update Both Pannels" />
                    <asp:Button ID="b2" runat="server" OnClick="one" Text="Update Single Pannel" />
                </fieldset>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="b1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
            <ContentTemplate>
                <fieldset style="width: 30%">
                    <legend>Panel - 2 </legend>
                    <asp:Label ID="label2" runat="server"></asp:Label>
                </fieldset>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="b2" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>

Button Click events are as follows,

protected void both(object sender, EventArgs e)
{
    label1.Text = DateTime.Now.ToLongTimeString();
    label2.Text = DateTime.Now.ToLongTimeString();
    UpdatePanel2.Update();

}
protected void one(object sender, EventArgs e)
{
    label1.Text = DateTime.Now.ToLongTimeString();
    label2.Text = DateTime.Now.ToLongTimeString();
    UpdatePanel2.Update();
}

The output remains same..Thanks in advance...

Was it helpful?

Solution

From Reference to MSDN If the UpdateMode property is set to Conditional, the UpdatePanel control’s content is updated when one of the following is true:

  1. When the postback is caused by a trigger for that UpdatePanel
    control.
  2. When you explicitly call the UpdatePanel control's Update method.
  3. When the UpdatePanel control is nested inside another UpdatePanel control and the parent panel is updated.
  4. When the ChildrenAsTriggers property is set to true and any child control of the UpdatePanel control causes a postback. Child controls of nested UpdatePanel controls do not cause an update to the outer UpdatePanel control unless they are explicitly defined as triggers for the parent panel.

So Set ChildrenAsTigger to false set asynchrnous trigger for your first update panel like below.

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
        <ContentTemplate>
        <fieldset style="width:30%">
        <legend>Panel - 1
        </legend>
        <asp:Label ID="label1" runat="server"></asp:Label>
        <asp:Button ID="b1" runat="server" OnClick="both" Text="Update Both Pannels" />
        <asp:Button ID="b2" runat="server" OnClick="one" Text="Update Single Pannel" />
        </fieldset>
        </ContentTemplate>
     <Triggers>
        <asp:AsyncPostBackTrigger ControlID="b1" EventName="Click" />
        </Triggers>
        </asp:UpdatePanel>
 <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
    <fieldset style="width:30%">
    <legend>Panel - 2
    </legend>
    <asp:Label ID="label2" runat="server"></asp:Label>
    </fieldset>
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="b2" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>

After that on click of B1 BUTTON update the sceond update panel explicitly...like below

protected void both(object sender, EventArgs e)
{
    label1.Text = DateTime.Now.ToLongTimeString();
    label2.Text = DateTime.Now.ToLongTimeString();
    UpdatePanel2.Update();
}



  protected void one(object sender, EventArgs e)
    {
        label1.Text = DateTime.Now.ToLongTimeString();
        label2.Text = DateTime.Now.ToLongTimeString();
       UpdatePanel2.Update();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top