KevinMc
Posted on Jan 16, 2008
(permalink)
Can the full code for the webservice used in the online demo for binding prometheus menu to a webservice be posted. The stub does not help much for determining what we need to return from the webservice.
Reply
Answer
Telerik Admin
Posted on Jan 17, 2008
(permalink)
Hello Kevin,
The full code is available in the App_Code folder of the QSF - ProductCategories.cs. Here is the menu-related code:
| using System; |
| using System.Web; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Collections.Specialized; |
| using System.Configuration; |
| using System.Data; |
| using System.Data.SqlClient; |
| using System.Threading; |
| using System.Web.Script.Services; |
| using System.Web.Services; |
| using System.Web.Services.Protocols; |
| using Telerik.Web.UI; |
| |
| |
| [ScriptService] |
| public class ProductCategories : WebService |
| { |
| [WebMethod] |
| public RadMenuItemData[] GetMenuCategories(RadMenuItemData item, object context) |
| { |
| IDictionary<string, object> contextDictionary = (IDictionary<string, object>) context; |
| |
| DataTable productCategories = GetProductCategories(contextDictionary["CategoryID"]); |
| |
| List<RadMenuItemData> result = new List<RadMenuItemData>(); |
| |
| foreach (DataRow row in productCategories.Rows) |
| { |
| RadMenuItemData itemData = new RadMenuItemData(); |
| itemData.Text = row["Title"].ToString(); |
| itemData.Value = row["CategoryId"].ToString(); |
| |
| if (Convert.ToInt32(row["ChildrenCount"]) > 0) |
| { |
| itemData.ExpandMode = MenuItemExpandMode.WebService; |
| } |
| |
| result.Add(itemData); |
| } |
| |
| return result.ToArray(); |
| } |
| |
| private DataTable GetProductCategories(object categoryId) |
| { |
| SqlConnection connection = new SqlConnection( |
| ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString); |
| |
| SqlCommand selectCommand = |
| new SqlCommand( |
| @" |
| SELECT |
| pc1.CategoryID, |
| pc1.Title, |
| ISNULL(pc2.ChildrenCount, 0) as ChildrenCount |
| FROM ProductCategories as pc1 |
| LEFT JOIN |
| ( |
| SELECT ParentId, COUNT(*) AS ChildrenCount |
| FROM ProductCategories |
| Group By (ParentId) |
| ) as pc2 |
| ON |
| pc1.CategoryId = pc2.ParentId |
| WHERE pc1.parentId = @parentId |
| ", |
| connection); |
| |
| selectCommand.Parameters.AddWithValue("parentId", categoryId); |
| |
| SqlDataAdapter adapter = new SqlDataAdapter(selectCommand); ; |
| |
| DataTable productCategories = new DataTable(); |
| adapter.Fill(productCategories); |
| |
| return productCategories; |
| } |
| } |
| |
| |
Greetings,
Tsvetomir Tsonev
the Telerik team
Reply