TreeView problems: limited AutoPostBack / LinkButton in Node / multiple trees

Last post 03-06-2009 1:45 AM by qwerty_key. 3 replies.

Sort Posts:

  • TreeView problems: limited AutoPostBack / LinkButton in Node / multiple trees

    08-07-2003, 7:51 AM
    • Member
      15 point Member
    • anon2anon
    • Member since 07-02-2002, 8:22 AM
    • Posts 3
    hi folks,

    I have some problems regarding the TreeView control.

    1. When AutoPostBack is set to true, all events (Expand/Collapse/SelectedIndexChange) cause a postback. But I only want the event SelectedIndexChange to be posted back, because Expand and Collapse needn't be handled on server. What should I do to disable Expand/Collapse to be posted back?

    2. Can I add LinkButtons or other Web Controls into a TreeNode? When the user select a TreeNode, I want a command to be issued, NOT a url to be opened. To do this, I tried to embed LinkButtons to TreeNodes but TreeNode isn't inherited from System.Web.UI.Control so I can't write code like this:

    LinkButton button = new LinkButton();
    ... (configure the button)
    TreeNode node = new TreeNode();
    node.Controls.Add(button);

    I think that if TreeNode inherits System.Web.UI.Control, it would be much more elegant than the current implementation.

    3. there are multiple dynamically created TreeViews on a single page. When users select a TreeNode in one of these TreeViews, the event SelectedIndexChange only contains the index of new/old nodes, how could I know which TreeView the selected TreeNode belongs to?
    If the event SelectedIndexChange also contains the ID of the selected TreeNode (but it doesn't), the problem can be solved easily.


    btw: I had requested these new features in another post
    http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=300392
  • Re: TreeView problems: limited AutoPostBack / LinkButton in Node / multiple trees

    11-20-2003, 12:37 PM
    I have had the same problem.
    I managed to find a workaround, not very elegant though. Code is in C#

    in .aspx
    make Treeview1.AutoPostback = false

    add an hidden input field (tbSelectedNode), don't forget to make it RunAtServer
    add a asp:Button (btnPost), I made this 1px high 1px wide so you can't see it

    add a javascript function in the HEAD section for the events to handle
    <script type="text/javascript">
    function nodepostback()
    {
    var postback = document.getElementById('btnPost');
    var hidden = document.getElementById('tbSelectedNode');
    var treeview = document.getElementById('tvMeterGroups');
    if(hidden != null && treeview != null)
    {
    if(*** enter your condition for posting here, if you want one ***)
    {
    hidden.value = treeview.selectedNodeIndex;
    postback .click();
    }
    }
    return false;
    };
    </script>

    in code behind .aspx.cs
    add the handlers for the events you want to handle, in the Page_Load like this:
    Treeview1.Attributes.Add("onselectedindexchange", "javascript:nodepostback();");

    add a page private static string variable:
    private static string strSelectedNode = "";

    add a click handler for the button
    private void btnPost_Click(object sender, System.EventArgs e)
    {
    strSelectedNode = tbSelectedNode.Value;
    ....
    ....
    }

    add PreRender for the treeview, to ensure the selection gets made
    private void Treeview1_PreRender(object sender, System.EventArgs e)
    {
    Treeview1.SelectedNodeIndex = strSelectedNode;
    }



    The above worked for me when all I wanted was 'onselectedindexchange' postback.

    I tried 'return false' handlers for the other events, when I had AutoPoasback=true, but that caused the control to lose collapsed/expanded info.
    I tried to remove attributes for the events I didn't want posted back in PreRender but that didn't remove them when I viewed source.
    This was all I could get to work.

    I hope this may help others who have similar problems.
  • Re: TreeView problems: limited AutoPostBack / LinkButton in Node / multiple trees

    03-11-2004, 10:52 PM
    Hi!

    I read in your post that you've added multiple treeviews in a page successfully. I am using treeview control in my Sharepoint Webparts (individual application which you can add in a page), i have created multiple webparts which uses the treeview to display different datas. I have try add multiple webparts in one page. But as soon as I add another webpart with treeview control in it. one of them stops displaying treeview!!! it's really wierd. could you please help me out in this one? what am i doing wrong? ie. how you are diming the treeivew and instanciating the control etc.. proxy settings.. etc.

    Any help in this will be much appriciated.

    Regards,
    Nilesh
  • Re: TreeView problems: limited AutoPostBack / LinkButton in Node / multiple trees

    03-06-2009, 1:45 AM
    • Member
      262 point Member
    • qwerty_key
    • Member since 02-27-2009, 12:00 AM
    • Posts 48

     hi

    I could not find any attribute like Autopostback for asp.net treeview control.

    To disable Expand/Collapse to be posted back , check if its EnableClientScript  attribute is set to true.

    To add LinkButtons or other Web Controls into a TreeNode, please refer this:

    http://forums.asp.net/p/1372883/2876414.aspx

     hope this helps......

Page 1 of 1 (4 items)