[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. Advertising

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. Ruby

    SelectedIndexChange

    Ruby, Jan 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    347
  2. Jay
    Replies:
    1
    Views:
    935
    Natty Gur
    Mar 4, 2004
  3. MBhat
    Replies:
    2
    Views:
    9,934
    MBhat
    Apr 29, 2004
  4. steven shingler

    treeview SelectedIndexChange problem

    steven shingler, Jul 25, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    146
    steven shingler
    Jul 28, 2003
  5. Phil

    Help - Treeview - SelectedIndexChange Event

    Phil, Apr 29, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    284
Loading...

Share This Page