This is a migrated thread and some comments may be shown as answers.

Method failed in usercontrol

1 Answer 131 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
TIGER
Top achievements
Rank 1
TIGER asked on 02 Oct 2011, 01:33 PM

Hi,
  I implemented a treeview using webservice in a web page and it worked ok. the treeview is simple as follows:                      

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CategoryTreeView.aspx.cs" Inherits="iBOOKSTORE.CategoryTreeView" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
   
 
                <telerik:RadTreeView ID="tvClass" runat="server" Height="400px" Width="500px"
                      Skin="Windows7" CheckBoxes="True" CheckChildNodes="True">
                    <WebServiceSettings Path="CategoryTreeView.aspx" Method="GetClassItems" />
                </telerik:RadTreeView>
     
    </div>
    </form>
</body>
</html>

   
the webmethod "GetClassItems" worked fine.
but when I convert it into a usercontrol like the following:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserTreeView.ascx.cs" Inherits="iBOOKSTORE.UserTreeView" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
    <div>
   
                <telerik:RadTreeView ID="tvClass" runat="server" Height="400px" Width="500px"
                      Skin="Windows7" CheckBoxes="True" CheckChildNodes="True">
                    <WebServiceSettings Path="UserTreeView.ascx" Method="GetClassItems" />
                </telerik:RadTreeView>
     
    </div>

there is no difference between server side Methods. And I drag this usercontrol  "UserTreeView.ascx" and drop it on a web page at the same directory. The treeview loaded ok by calling a server side function which returned a datatable. but when I click open a node, an error message popped up "GetClassItems Failed". I tried to set a break point in the WebMethod "GetClassItems", the execution never got inside. I suppose this must have something to do with the Path property. I purposely place the host page and usercontrol in the same directory, but no matter what I tried the same error occured. could you provide some advice please. 
Thank you.
I send the server side code as well

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.Services;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;
 
namespace iBOOKSTORE
{
    public partial class UserTreeView : System.Web.UI.UserControl
    {
        static DataTech.AspNet.Data.dtAspNetDataSource ds = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ds = new DataTech.AspNet.Data.dtAspNetDataSource();
                LoadRootNodes(tvClass, TreeNodeExpandMode.WebService);
            }
        }
 
        private static void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DPTCODE,DPTNAME FROM DEPARTMENT WHERE LEFT(DPTCODE,1) <>'-' ORDER BY DPTCODE ";
            cmd.CommandTimeout = 0;
 
 
            DataTable data = GetData(cmd);
 
            foreach (DataRow row in data.Rows)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = row["DPTNAME"].ToString();
                node.Value = row["DPTCODE"].ToString();
                node.ExpandMode = expandMode;
                treeView.Nodes.Add(node);
            }
        }
 
        private static DataTable GetData(SqlCommand sqlcmd)
        {
            return (ds.GetDataRows(sqlcmd, "", "", 0, int.MaxValue));
        }
 
        private static DataTable GetChildNodes(string parentId)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
 
            cmd.CommandText = "SELECT SUBSTRING(pc1.CLSCODE,1," + (parentId.Trim().Length + 1).ToString() + ") AS PCODE,pc1.PCODE,pc1.CLSCODE,pc1.CLSNAME,ISNULL(pc2.ChildrenCount, 0) as ChildrenCount FROM CLASS AS pc1 " +
                " LEFT JOIN ( SELECT PCODE, COUNT(*) AS ChildrenCount FROM CLASS GROUP BY PCODE) AS pc2" +
                " ON SUBSTRING(pc1.CLSCODE,1," + (parentId.Trim().Length + 1).ToString() + ")=pc2.PCODE WHERE pc1.PCODE='" + parentId.Trim() + "'";
 
 
            cmd.CommandTimeout = 0;
            return GetData(cmd);
        }
 
 
 
 
        private void UpdateLoadingStatusPosition(TreeViewLoadingStatusPosition statusPosition)
        {
 
            tvClass.LoadingStatusPosition = statusPosition;
        }
 
 
        [WebMethod]
        public static RadTreeNodeData[] GetClassItems(RadTreeNodeData node)
        {
 
            DataTable data = GetChildNodes(node.Value);
 
            List<RadTreeNodeData> result = new List<RadTreeNodeData>();
            foreach (DataRow row in data.Rows)
            {
 
                RadTreeNodeData childNode = new RadTreeNodeData();
                childNode.Text = row["CLSNAME"].ToString();
                childNode.Value = row["PCODE"].ToString();
                if (Convert.ToInt32(row["ChildrenCount"]) > 0)
                {
                    childNode.ExpandMode = TreeNodeExpandMode.WebService;
                }
                result.Add(childNode);
            }
 
            return result.ToArray();
        }
 
 
    }
}

 

1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 05 Oct 2011, 02:53 PM
Hello Tiger,

Calling page method from user controls is not supported by the asp.net framework. In your case you should use a web service in order to achieve the desired behavior.

Please refer to the following forum thread discussing this limitation.

Regards,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
TreeView
Asked by
TIGER
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or