My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
TreeViewASPNET  
Evitar el PostBack en un TreeView en ASPNET
Updated Feb 4, 2010 by pepeluxx

Introduction

En la parte web es un coñazo el PostBack porque, aparte de subir la página y perder el foco, los fileuploader de Ajax se bloquean. Para solocionarlo:

Details

Para rellenar el árbol con las categorías he creado una función recursiva:

            TreeView arbol = new TreeView();
            TreeNode nodoRaiz;
            TreeNode nodoPadre;

            nodoRaiz = new TreeNode("Raíz", "0");
            arbol.Nodes.Add(nodoRaiz);

            // rellenamos el arbol de categorias de forma recursiva
            nodoPadre = crearArbol(nodoRaiz, 0);
            nodoRaiz.ChildNodes.Add(nodoPadre);



        // funcion recursiva que rellena el arbol de categorias
        private TreeNode crearArbol(TreeNode nodoRaiz, int nodo)
        {
            TreeNode nodoPadre;

            try
            {
                ArrayList listadoCat = new ArrayList();
                listadoCat = CPCategorias.listado(nodo);

                foreach (DTOCategoria dtocat in listadoCat)
                {
                    nodoPadre = new TreeNode(dtocat.Nombre, Convert.ToString(dtocat.Id));
                    nodoPadre.NavigateUrl = "javascript:labelCat('" + dtocat.Nombre + "')";
                    nodoRaiz.ChildNodes.Add(crearArbol(nodoPadre, dtocat.Id));
                }
            }
            catch { }

            return nodoRaiz;
        }

Para evitar el PostBack he creado el NavigateUrl que apunta a un JS que lo único que hace es identificar el nodo seleccionado:

     nodoPadre.NavigateUrl = "javascript:labelCat('" + dtocat.Nombre + "')";

La funcion JS que esta en el aspx es simplemente un alert que avisa de que hemos seleccionado la categoría:

<script type="text/javascript">
function labelCat(nodo) {
    alert('Ha seleccionado la categoría ' + nodo);
}
</script>
Comment by juanriv...@gmail.com, Feb 27, 2012

Otra solución muy sencilla es a los nodos padres colocar nodo.SelectAction? = TreeNodeSelectAction?.Expand y a los hijos nodo1.SelectAction? = TreeNodeSelectAction?.None

Powered by Google Project Hosting