[Solution] Treeview without Autopostback but with SelectedIndexChange

Discussion in 'ASP .Net Web Controls' started by Stefan, Jul 18, 2003.

  1. Stefan

    Stefan Guest

    Hi,

    I developed an ASP.NET Web Application with many User Controls in it. One of
    that including
    the MS Treeview Control. Since i wanted the Control to track the
    SelectedIndex, i implemented
    a server-side eventhandler to do so.

    One of the drawbacks of the Treeview Control (as others mentioned too) is
    its Autopostback-
    functionality (On: Postback on all events including expanding of nodes and
    so on / Off: All Postbacks
    off). So i searched the web for a solution, to let the Treeview Control fire
    the postback only in case of
    SelectedIndexChange event but not in other cases. (relevant performance
    costs)

    The Solution posted by Patrick Cole (Microsoft Developer Support) was
    perfectly matching but
    could only be used in applications implementing the TreeView Control in
    Webpages (but not in
    User Controls) because of the need to modify the <body> tag.

    Here this Solution:
    http://groups.google.com/groups?hl=...w=1&selm=Q1yaFpiPCHA.1828@cpmsftngxa08&rnum=6

    I modified this solution in such way, that it can be used "everywhere" (i
    tested it only in my user control), here
    the code:

    ======================================================
    public abstract class TheUserControl : System.Web.UI.WebControl
    {
    protected Microsoft.Web.UI.WebControls.TreeView ctlTree;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Page.RegisterClientScriptBlock("TreeEvent",
    this.BuildClientsideEventhandler());
    this.ctlProjectTree.Attributes.Add("onselectedindexchange",
    "javascript:treeEvent();");
    }

    private String BuildClientsideEventhandler()
    {
    String nl = Environment.NewLine;
    String strRef = Page.GetPostBackEventReference(ctlTree);
    String strControl = this.ctlProjectTree.UniqueID.Replace(":", "_");
    return "<script language=\"JavaScript\">" + nl +
    "<!-- " + nl +
    " function treeEvent() {" + nl +
    " if (event.oldTreeNodeIndex != event.newTreeNodeIndex) " + nl +
    " " + strControl + ".queueEvent('onselectedindexchange',
    event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex); " + nl +
    " window.setTimeout('" + strRef.Replace("'","\\'") + "', 0,
    'JavaScript'); " + nl +
    " } " + nl +
    "// --> " + nl +
    "</script>";
    }
    }
    ===========================================================

    You don't have to do anything additionaly..

    Sincerlely,
    Stefan Kals
    mailto:
    http://www.css-web.net
     
    Stefan, Jul 18, 2003
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex D.

    treeview and autopostback

    Alex D., Oct 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,938
    Alejandro Penate-Diaz
    Oct 30, 2005
  2. Dmitry Perets
    Replies:
    0
    Views:
    566
    Dmitry Perets
    Mar 20, 2007
  3. Dmitry Perets

    TreeView checkboxes AutoPostBack with AJAX

    Dmitry Perets, Mar 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    932
    bruce barker
    Mar 20, 2007
  4. Stu

    Treeview JS error when AutoPostBack=True...

    Stu, Jul 23, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    199
  5. steven shingler

    treeview SelectedIndexChange problem

    steven shingler, Jul 25, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    277
    steven shingler
    Jul 28, 2003
  6. Jim M
    Replies:
    0
    Views:
    244
    Jim M
    Jan 14, 2004
  7. Phil

    Help - Treeview - SelectedIndexChange Event

    Phil, Apr 29, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    429
  8. Replies:
    2
    Views:
    573
Loading...