简体   繁体   中英

Communicate between UpdatePanels ASP.Net

I have placed a treeView in One Update Panel and Each View in one update Panel something like this


   <UpdatePanel id="UP1">
     <ContentTemplate>
       <TreeView/>
     </ContentTemplate>
  </UpdatePanel>

   <MultiView>
    <UpdatePanel id="UP2">
      <View1/>
     </UpdatePanel>

Now I want to know how I can make sure When I click any node of TreeView respective Views should get displayed

Another way of approaching it would be to call UP2.Update() from the codebehind if you have code for the click event of the Treeview. Remember, UP2 needs to have its RenderMode set to Conditional for this to work. Hope it helps

Add an AsyncPostBackTrigger to the second updatePanel, so it gets updated when the TreeView Click event is fired.

<Asp:UpdatePanel id="UP2">
   <View1/>
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="TreeView1" EventName="Click" />
   </Triggers>
</Asp:UpdatePanel>

OK, Here Is a working Example.

The Markup:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table style="width: 100%;">
        <tr>
            <td>
                <asp:UpdatePanel ID="upTreeView" runat="server">
                    <ContentTemplate>
                        <asp:TreeView ID="TreeView1" runat="server" 
                            onselectednodechanged="TreeView1_SelectedNodeChanged">
                            <Nodes>
                                <asp:TreeNode Text="GrandFather" Value="GrandFather">
                                    <asp:TreeNode Text="Father" Value="Father">
                                        <asp:TreeNode Text="Son" Value="Son"></asp:TreeNode>
                                    </asp:TreeNode>
                                </asp:TreeNode>
                            </Nodes>
                        </asp:TreeView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
            <td>
                <asp:UpdatePanel ID="upView" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="TreeView1" 
                            EventName="SelectedNodeChanged" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
        </tr>
     </table> 

The code behind:

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Label1.Text = TreeView1.SelectedValue;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM