Call DirectMethod from a TreePanel

  1. #1

    Call DirectMethod from a TreePanel

    Happy New Year 2011.

    I have a masterpage with a left navigation (Treepanel) in a usercontrol and a main content grid on a different usercontrol. I created the TreePanel from the codebehind which displays all the departments and the categories inside the departments. When I click on a department the categories are displayed but I cannot seem to understand how to call a DirectMethod on the main content usercontrol. I want to be able to pass the department or the category id to the DirectMethod that is on the main content usercontrol and refresh the grid.

    Line 40 of MenuTree.ascx.cs is I think the start.

    Here is the live start of the project without line 40:
    http://asiancrafters.com/Catalog.aspx

    This is what I have:

    MasterPage Code:
    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="web.master.cs" Inherits="web1" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <%@ Register src="UserControl/MenuTree.ascx" tagname="MenuTree" tagprefix="uc1" %>
    <%@ Register src="UserControl/RightColumn.ascx" tagname="RightColumn" tagprefix="uc2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder id="head" runat="server">
        </asp:ContentPlaceHolder>
        <link href="Theme/Style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <ext:ResourceManager runat="server" Theme="Gray" />
        <form id="form1" runat="server">
            <div class="page">
                <div class="headerRow">
                    <img src="images/logo.jpg" alt="Go to the Homepage" />
                </div>
                
                <div class="mainContentRow">
                    <div class="leftColumn">
                        <uc1:MenuTree ID="leftNavigation" runat="server" />
                    </div>
    
                    <div class="centerColumn">
                        <asp:ContentPlaceHolder ID="body" runat="server" />
                    </div>
    
                    <div class="rightColumn">
                        <uc2:RightColumn ID="rightColumn" runat="server" />
                    </div>
                </div>
            </div>
        </form>
    </body>
    </html>
    MenuTree.ascx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Ext.Net;
    
    
    public partial class UserControl_MenuTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ext.Net.TreePanel tree = new Ext.Net.TreePanel();
            
            this.PlaceHolder1.Controls.Add(tree);
    
            tree.ID = "TreePanel1";
            tree.Width = Unit.Pixel(220);
            tree.Height = Unit.Pixel(450);
            tree.Icon = Icon.BookOpen;
            tree.Title = "Catalog";
            tree.AutoScroll = true;
                                   
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Departments");
            root.Expanded = true;
            tree.Root.Add(root);  
             
            CatalogBLL catalogBLL = new CatalogBLL();
            List<Department> dept = new List<Department>();
            dept = catalogBLL.GetDepartmentTree();
    
            if (dept != null && dept.Count > 0)
            {
                foreach (Department department in dept)
                {
                    Ext.Net.TreeNode deptNodes = new Ext.Net.TreeNode(department.Name, Icon.ApplicationSideExpand);
                    deptNodes.NodeID = department.ID.ToString();
                    root.Nodes.Add(deptNodes);
                    deptNodes.Listeners.Click.Handler = "Ext.net.DirectMethods.department_click(deptNodes.NodeID);";
                                                    
                    CatalogBLL categoryBLL = new CatalogBLL();
                    List<Category> cat = new List<Category>();
                    cat = categoryBLL.GetCategoriesBy(Convert.ToInt32(department.ID));
    
                    if (cat != null && cat.Count > 0)
                    {
                        foreach (Category category in cat)
                        {
                            Ext.Net.TreeNode catNodes = new Ext.Net.TreeNode(category.Name, Icon.Pencil);
                            catNodes.NodeID = category.ID.ToString();
                            deptNodes.Nodes.Add(catNodes);
                        }
                    }
                }
            }
        }
    }
    CatalogList.ascx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Ext.Net;
    
    public partial class UserControl_CatalogList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CatalogBLL catalogBLL = new CatalogBLL();
                List<Catalog> catalog = new List<Catalog>();
                catalog = catalogBLL.GetAllProducts();
    
                dlCatalog.DataSource = catalog;
                dlCatalog.DataBind();
            }
        }
    
        [DirectMethod]
        protected void department_click(Int32 deptID)
        {
            if (deptID > 0)
            {
                CatalogBLL catalogBLL = new CatalogBLL();
                List<Catalog> catalog = new List<Catalog>();
                catalog = catalogBLL.GetAllProducts();
    
                dlCatalog.DataSource = catalog;
                dlCatalog.DataBind();
                dlCatalog.Update();
            } 
        }
    
        [DirectMethod]
        protected void category_click(object sender, DirectEventArgs e)
        {
    
        }
    
        [DirectMethod]
        protected void search_click(object sender, DirectEventArgs e)
        {
            if (txtSearch.Text.Trim() != "")
            {
                ImageChooserDialog.Title = "Search Results";
    
                CatalogBLL catalogBLL = new CatalogBLL();
                List<Catalog> catalog = new List<Catalog>();
                catalog = catalogBLL.SearchProducts(txtSearch.Text.Trim(), 1);
                dlCatalog.DataSource = catalog;
                dlCatalog.DataBind();
                dlCatalog.Update();
    
            }
        }
    }
    Any help would be really appreciated. Thank you.
  2. #2
  3. #3

    Get an error: Microsoft JScript runtime error: 'Ext.net.DirectMethods.UCDepartment' is null or not an object

    Hi Vladimir,

    Thank you for your reply. I had seen the example and had tried working on it before but got an error.

    Basically the difficulty I am facing is that I do not know how to write the line to call the method and pass the node id.

    deptNodes.Listeners.Click.Handler = "Ext.net.DirectMethods.UCDepartment.department_click(deptNodes.NodeID);";
    Error I get:
    "Microsoft JScript runtime error: 'Ext.net.DirectMethods.UCDepartment' is null or not an object"
    I did register the UserControl in MenuTree.ascx page.

    Here is what I had tried doing from the example you gave me:

    MenuTree.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MenuTree.ascx.cs" Inherits="UserControl_MenuTree" %>
    <%@ Register src="CatalogList.ascx" tagname="CatalogList" tagprefix="uc" %>
    
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
    
    <uc:CatalogList ID="UCDepartment" runat="server" Visible="false" />
    MenuTree.ascx.cs (line 40 is where I call the method)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Ext.Net;
    
    
    public partial class UserControl_MenuTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ext.Net.TreePanel tree = new Ext.Net.TreePanel();
            
            this.PlaceHolder1.Controls.Add(tree);
    
            tree.ID = "TreePanel1";
            tree.Width = Unit.Pixel(220);
            tree.Height = Unit.Pixel(450);
            tree.Icon = Icon.BookOpen;
            tree.Title = "Catalog";
            tree.AutoScroll = true;
                                   
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Departments");
            root.Expanded = true;
            tree.Root.Add(root);  
             
            CatalogBLL catalogBLL = new CatalogBLL();
            List<Department> dept = new List<Department>();
            dept = catalogBLL.GetDepartmentTree();
    
            if (dept != null && dept.Count > 0)
            {
                foreach (Department department in dept)
                {
                    Ext.Net.TreeNode deptNodes = new Ext.Net.TreeNode(department.Name, Icon.ApplicationSideExpand);
                    deptNodes.NodeID = department.ID.ToString();
                    root.Nodes.Add(deptNodes);
                    deptNodes.Listeners.Click.Handler = "Ext.net.DirectMethods.UCDepartment.department_click(deptNodes.NodeID);";
                                                    
                    CatalogBLL categoryBLL = new CatalogBLL();
                    List<Category> cat = new List<Category>();
                    cat = categoryBLL.GetCategoriesBy(Convert.ToInt32(department.ID));
    
                    if (cat != null && cat.Count > 0)
                    {
                        foreach (Category category in cat)
                        {
                            Ext.Net.TreeNode catNodes = new Ext.Net.TreeNode(category.Name, Icon.Pencil);
                            catNodes.NodeID = category.ID.ToString();
                            deptNodes.Nodes.Add(catNodes);
                        }
                    }
                }
            }
        }
    }
  4. #4
    Hi,

    Firstly, access modifier of DirectMethod must be public and only public.

    Example
    [DirectMethod]public void department_click(Int32 deptID)
    {
        ...
    }
    Secondly, if you call within an user control which is defined in this control namespace must be #{DirectMethods}.

    Thirdly, there is no deptNodes.NodeID on client side.

    So, please try the following
    deptNodes.Listeners.Click.Handler = "#{DirectMethods}.department_click(this.id);";

Similar Threads

  1. Show a load mask with a DirectMethod call
    By ashwinrath in forum 1.x Help
    Replies: 2
    Last Post: Apr 21, 2012, 6:35 PM
  2. [CLOSED] Call directMethod in ext:window Content
    By supera in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 20, 2012, 6:51 PM
  3. simple DirectMethod call not working?
    By phinoppix in forum 1.x Help
    Replies: 2
    Last Post: Jul 05, 2011, 12:12 PM
  4. [CLOSED] [1.0] DirectMethod call on IFrame
    By MP in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 24, 2010, 6:42 PM
  5. how to call DirectMethod from grid
    By harshad.jadhav in forum 1.x Help
    Replies: 2
    Last Post: Jul 10, 2010, 8:58 AM

Tags for this Thread

Posting Permissions