Showing the newly added node in asp.net treeview without the need for scrolling

Last post 11-19-2007 5:20 AM by Amanda Wang - MSFT. 5 replies.

Sort Posts:

  • Showing the newly added node in asp.net treeview without the need for scrolling

    11-09-2007, 9:55 AM
    • Loading...
    • Sam964
    • Joined on 08-11-2004, 10:47 AM
    • Posts 76

    Hello,

    I have a treeview in a panel which has a scrollbars property. I allow the user to add new nodes to the tree which adds a new row to the db table. I am able to select the newly added node in the table. But if the newly added node is not in the view area of the user, the user has to scroll down to see the newly added node. Is there a way I can implement it so that the user doesn't need to scroll to find the node. I want to show the newly added node and pre-scroll it for the user.

    I have got this code..

    var treePanel = document.getElementById('<%= treePanel.ClientID %>');

    var selectedNode = '<%=XXXXTV.ClientID %>' + '_SelectedNode';

    var elem = document.getElementById(selectedNode);if(elem != null)

    {

    var node = document.getElementById(elem.value);

    if(node != null)

    {

    alert(node);

    node.scrollIntoView(
    true);

    treePanel.scrollLeft = 0;

     

    }

    }

    First of all, I am not sure this code works. If it works, I am not sure where and when should I invoke this code. Can somebody please help?

    Thanks a lot

     

     

  • Re: Showing the newly added node in asp.net treeview without the need for scrolling

    11-12-2007, 1:22 AM

    Hi,

    You can invoke the javascript code after the new nodes added, you can use the Page.RegisterStartupScript Method to call the javascript function. This method allows ASP.NET server controls to emit client-side script blocks in the page.

    The more you can read more in this MSDN article:  http://msdn2.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript(VS.71).aspx

    Hope it helps.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Yours sincerely,
    Amanda Wang
    Microsoft Online Community Support
  • Re: Showing the newly added node in asp.net treeview without the need for scrolling

    11-12-2007, 10:26 AM
    • Loading...
    • Sam964
    • Joined on 08-11-2004, 10:47 AM
    • Posts 76

    Hi Amanda,

    Thanks for your suggestion. I tried these steps but I am having a few issues.

    I register the script using Page.RegisterStartupScript. But how can I invoke that function? I have to first make sure that the new node is inserted into the db table, which I do on a button click event. Then I pull the nodes once again from the db so that I can select the newly added node. If I add an attribute to onclick of the button, am I guaranteed that first the button click event occurs (and server side code executes) and then finally, the javascript function is invoked?.

    I meant to say, how can I ensure that the javascript function is invoked last?

    Also for some reason, I don't see the script in the page source. I have user controls on the page and the treeview is in one user control and adding a node functionality is in another user control.

    Can you please get back to me?

    Thanks a lot!!

  • Re: Showing the newly added node in asp.net treeview without the need for scrolling

    11-13-2007, 10:42 PM

    Hi,

    You can call the javascript function after your code runs.

    About how to call JavaScript function from the codebehind, you can check this thread: http://forums.asp.net/t/1177991.aspx

     

    Hope it helps.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Yours sincerely,
    Amanda Wang
    Microsoft Online Community Support
  • Re: Showing the newly added node in asp.net treeview without the need for scrolling

    11-16-2007, 1:57 PM
    • Loading...
    • Sam964
    • Joined on 08-11-2004, 10:47 AM
    • Posts 76

    Hi Amanda,

    Thanks for your pointers. I did get how to invoke the script, but for some reason the script doesn't work..

    --For some reason elem.value in the below code is empty string.. I am using AJAX in this app. Whenever a treenode is selected and it's value set (by calling node.select() and assigning node.value=somevalue in the server side), why can't I access that elem by value using javascript after the values are set?  

    var treePanel = document.getElementById('<%= treePanel.ClientID %>');

    var selectedNode = '<%=XXXXTV.ClientID %>' + '_SelectedNode';

    var elem = document.getElementById(selectedNode);if(elem != null)

    {

    --Below line gives a null value.. 

    var node = document.getElementById(elem.value); if(node != null)

    {

    node.scrollIntoView(
    true);

    treePanel.scrollLeft = 0;

     

    }

    }

    Thanks a lot!!

  • Re: Showing the newly added node in asp.net treeview without the need for scrolling

    11-19-2007, 5:20 AM

    Hi,

    About how to get the selectednode from the client side, you can try to refer this article: ASP.NET TreeView Control & the Client's Browser

     

    Hope it helps.

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Yours sincerely,
    Amanda Wang
    Microsoft Online Community Support
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter