0

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...

Developer
  • 231
  • 4
  • 19
  • Your description is not clear. Please clear your explanation. – kst Jul 09 '13 at 08:39
  • no error. Clicking both the buttons displays time in both labels.. But, actual output expected is, only 1st label wants to update the current time if i click on the second button. Thanks for the reply. – Developer Jul 09 '13 at 08:43

1 Answers1

2

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();
    }
Amit Singh
  • 8,039
  • 20
  • 29
  • Thanks again :) This doesn't loads the `Label1`. `Label2` works correctly if the `both` button called. What to do? – Developer Jul 09 '13 at 08:51
  • @Developer couldn't get u? – Amit Singh Jul 09 '13 at 08:53
  • If I change `ChildrenAsTriggers="false", Label1` doesn't get affected in any case. It simply remains blank.? – Developer Jul 09 '13 at 08:55
  • click of b2 is fine right..on click of b1...it doesn't update the label2 right..try my modified code – Amit Singh Jul 09 '13 at 09:03
  • sorry it remains unchanged. I was working with another sample simultaneously where i made some modification. there i didnt print in panel1. but this sample output remains unchanged. I sorry for the previous comment... – Developer Jul 09 '13 at 09:04
  • i edited the comment, plz check that. even after ur suggested modification the output remains same, i.e always updates both the time.. – Developer Jul 09 '13 at 09:07
  • The current time is displayed in both the label whenever i click any of the button. – Developer Jul 09 '13 at 09:09
  • @Developer trigger for second update panel wil b like this...`` – Amit Singh Jul 09 '13 at 09:16
  • yeah I saw, updated, checked. But result remains the same. I have updated my actual whole code, u too verify it.. – Developer Jul 09 '13 at 09:22
  • @Developer your second panel still have triiger as b1 – Amit Singh Jul 09 '13 at 09:24
  • u set that only, me too had switched between both b1 and b2. that doesnt made the difference to the output. – Developer Jul 09 '13 at 09:28
  • forupdate[panel1 trigerr will be b1 and updatepanel2 trigger will be b2...see updated answer – Amit Singh Jul 09 '13 at 09:30
  • yes i said, i tried with this too. But, didn't work. BTW, Having one more doubt,u said page_Load will be called always,likewise does page reloads or refresh while we use update panel.? – Developer Jul 09 '13 at 09:34
  • @Developer try the code now it will work...on click of button2 event write UpdatePanel2.Update(); like above see carefully....copy it all and paste it it will work – Amit Singh Jul 09 '13 at 09:35
  • I did, copied and pasted as it is and executed. Still no change in the output. :( No clues. What have been wrong??? – Developer Jul 09 '13 at 09:39
  • post your updated code that u r nunning now.?..it works well i tried my self – Amit Singh Jul 09 '13 at 09:40
  • updated. I'm running in .net framework 2.0, and visula studio 2005. will it makes any difference.? – Developer Jul 09 '13 at 09:43
  • @Developer yes update panel class work with after 3.5 version.So pls upgrade to atleast vs2008.The code is fine. – Amit Singh Jul 09 '13 at 09:48
  • OMG, then cant work in vs 2005? Its the organization's software, no other way? – Developer Jul 09 '13 at 09:50
  • we have no admin rights to instal anything. And one more doubt, u said page_Load will be called always,likewise does page reloads or refresh while we use update panel.? – Developer Jul 09 '13 at 09:54
  • can not understand u....updatepanel updatemode="Conditional" is available in 3.5 or above.... – Amit Singh Jul 09 '13 at 09:55
  • Oh. Using update panel always reloads the page is it fact or it is a bug? – Developer Jul 09 '13 at 09:57
  • no its property it not a bug....it runs full page life cycle on server.it doesnt reload it calls page_load even only....but render the portion that come under update panel. – Amit Singh Jul 09 '13 at 09:59
  • how to check that page gets reloaded or not.? I guess the page gets reloaded. – Developer Jul 09 '13 at 10:01
  • not if u update the lable that doesn not belongs to update panel and u update it on asynchronous post u will see it doesn't get updated. – Amit Singh Jul 09 '13 at 10:03
  • @Developer becz you are using .net 2.0 thats y – Amit Singh Jul 09 '13 at 10:08