开发者

client side script for treeview events

开发者 https://www.devze.com 2023-02-01 21:34 出处:网络
I have a Treeview with \"populateOnDemand\" set to true. I want this treeview to keep its state (nodes expanded/collapsed) from one page to another. Viewstate doesn\'t work because the treeview is dif

I have a Treeview with "populateOnDemand" set to true. I want this treeview to keep its state (nodes expanded/collapsed) from one page to another. Viewstate doesn't work because the treeview is different for each page. Here is my code at the moment.

ASPX page :

<asp:TreeView
          ID="arbre"
          EnableViewState="true"
          PopulateNodesFromClient="true"
          runat="server" />

Code behind :

Protected Sub arbre_TreeNodeCollapsed(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles arbre.TreeNodeCollapsed
    CType(Session("listeNoeudsOuverts"), Hashtable)(e.Node.Value) = True
End Sub

Protected Sub arbre_TreeNodeExpanded(ByVal s开发者_如何学JAVAender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles arbre.TreeNodeExpanded
    CType(Session("listeNoeudsOuverts"), Hashtable)(e.Node.Value) = False
End Sub


Protected Sub arbre_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles arbre.TreeNodePopulate
    // code to populate nodes
    CType(Session("listeNoeudsOuverts"), Hashtable)(e.Node.Value) = True
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Session("listeNoeudsOuverts") Is Nothing Then
        Session("listeNoeudsOuverts") = New Hashtable()
    End If
    // other stuff
End Sub

This works well, buf I wish I could avoid the postback everytime the user expands or collapses a node. I know I can set EnebleClientScript to true and manage the events client side in Javascript but I won't be able to use my session variable anymore. Is there a way to achieve this ?

Thank you


I will answer myself to a certain extent.

I've done some tests and research, EnableClientScript is true by default, and indeed means that expand and collapse actions are processed client side, by Javascript code that is automaticalt generated by the .Net framework. You can't edit it.

Apparently, if you need to add custom actions when a user expands or collapses anode, you have to use TreeNodeExpanded and TreeNodeCollapsed events, like i did above, and can't avoid postbacks because they are triggered server-side.

0

精彩评论

暂无评论...
验证码 换一张
取 消