| 0. | namespace YourNamespace |
| 1. | { |
| 2. | | public class YourClass : System.Web.UI.Page |
| 3. | | { |
| 4. | | | //smPriceList is the SlidingMenu WebControl declared in XML/HTML view |
| 5. | | | protected Webmodelling.Controls.Menues.SlidingMenu smPriceList; |
| 6. | | |
| 7. | | | private void Page_Load(object sender, System.EventArgs e) |
| 8. | | | { |
| 9. | | | | //The XmlDocument is an appropriate object for our use, it can read a physical path or a full URL (if your XML |
| 10. | | | | //source is remote) and parse the XML into a DOM and also supplying appropriate methods including XPath understanding. |
| 11. | | | | string physicalPath_xmlSource = MapPath("/data/PriceList.xml"); |
| 12. | | | | XmlDocument xDoc = new XmlDocument(); |
| 13. | | | | xDoc.Load(physicalPath_xmlSource); |
| 14. | |
| 15. | | | | XmlElement xeProducts = xDoc.DocumentElement; //xeProducts is now the products element in the XML source |
| 16. | | | | XmlNodeList xnlProductCategories = xeProducts.ChildNodes;// confused? check the XML source |
| 17. | |
| 18. | | | | smPriceList.DataSource = xnlProductCategories; |
| 19. | | | | //tell what function should listen for the GroupDataBound event (GroupDataBound will be raised on each item in the DataSource (each product category) |
| 20. | | | | smPriceList.GroupDataBound += new SlidingMenuGroupEventHandler(smPriceList_GroupDatabound); |
| 21. | | | | //ok raise the events (the function smPriceList_GroupDatabound will run on each product category) |
| 22. | | | | smPriceList.DataBind(); |
| 23. | | | } |
| 24. | |
| 25. | | | protected void smPriceList_GroupDatabound(object sender, SlidingMenuGroupEventArgs e) |
| 26. | | | { |
| 27. | | | | XmlNodeList dataSource = (XmlNodeList)smPriceList.DataSource; |
| 28. | | | | XmlNode xnCurrent = dataSource[e.GroupIndex]; |
| 29. | | | | //Here xnCurrent is the current product category (remember this function runs once for each product category) |
| 30. | |
| 31. | | | | Label lblProductCategory = (Label)e.GroupHeader.FindControl("lblProductCategory"); |
| 32. | | | | DataGrid dgProducts = (DataGrid)e.GroupCanvas.FindControl("dgProducts"); |
| 33. | |
| 34. | | | | string productCategory = xnCurrent.SelectSingleNode("categoryDisplayName").InnerText; |
| 35. | | | | lblProductCategory.Text = productCategory; |
| 36. | |
| 37. | | | | //The SlidingMenu is bound to the list of product categories, and for each product category I bind a DataGrid to the list of product category child items |
| 38. | | | | //xnCurrent.SelectNodes("item") is the child items (products) under the current product category |
| 39. | | | | dgProducts.DataSource = xnCurrent.SelectNodes("item"); |
| 40. | | | | //tell which function to call upon each product in the current product category |
| 41. | | | | dgProducts.ItemDataBound += new DataGridItemEventHandler(dgProducts_ItemDataBound); |
| 42. | | | | //ok, raise the ItemDataBound event for each item in the DataSource (each product) knowing that the dgProducts_ItemDataBound function will be called for these products |
| 43. | | | | dgProducts.DataBind(); |
| 44. | |
| 45. | | | | //for fun: open the current Group of the SlidingMenu, if the current product category is "TV" |
| 46. | | | | if (xnCurrent.Name == "TV") |
| 47. | | | | { |
| 48. | | | | | e.GroupHeader.GroupHeaderStyles.OpenThisGroupAtStartup = true; |
| 49. | | | | } |
| 50. | | | } |
| 51. | |
| 52. | | | private void dgProducts_ItemDataBound(object sender, DataGridItemEventArgs e) |
| 53. | | | { |
| 54. | | | | if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) |
| 55. | | | | { |
| 56. | | | | | XmlNodeList dataSource = (XmlNodeList)((DataGrid)sender).DataSource; |
| 57. | | | | | XmlNode xnCurrent = dataSource[e.Item.ItemIndex]; |
| 58. | | | | | //Here xnCurrent is the current item (which is a product) |
| 59. | |
| 60. | | | | | System.Web.UI.WebControls.Image imgProductImage = (System.Web.UI.WebControls.Image)e.Item.FindControl("imgProductImage"); |
| 61. | | | | | HyperLink hplProductName = (HyperLink)e.Item.FindControl("hplProductName"); |
| 62. | | | | | Label lblProductPrice = (Label)e.Item.FindControl("lblProductPrice"); |
| 63. | |
| 64. | | | | | //If I am not sure, that the imageFile node exists, I use GetInnerText to avoid raising an error if I ask for InnerText on a null reference (imageFile will be a null reference if it does not exist in the Xml source) (in large Xml scenaries a function like GetInnerText comes in handy, here it is not really necessary) |
| 65. | | | | | string productImageFile = GetInnerText(xnCurrent.SelectSingleNode("imageFile")); |
| 66. | | | | | if (productImageFile != String.Empty) |
| 67. | | | | | { |
| 68. | | | | | | string productImageUrl = "/images/slidingmenu/example_Pricelist/" + productImageFile; |
| 69. | | | | | | imgProductImage.ImageUrl = productImageUrl; |
| 70. | | | | | } |
| 71. | | | | | else |
| 72. | | | | | { |
| 73. | | | | | | imgProductImage.Visible = false; |
| 74. | | | | | } |
| 75. | |
| 76. | | | | | string productName = xnCurrent.SelectSingleNode("displayName").InnerText; |
| 77. | | | | | string productPrice = xnCurrent.SelectSingleNode("price").InnerText; |
| 78. | |
| 79. | | | | | hplProductName.Text = productName; |
| 80. | | | | | //Here I don't provide a real link to the product, however in real use, you will of course link to another page and append a productId or even take the URL from the XML file. |
| 81. | | | | | hplProductName.NavigateUrl = "pricelist.aspx"; |
| 82. | | | | | lblProductPrice.Text = productPrice; |
| 83. | | | | } |
| 84. | | | } |
| 85. | |
| 86. | | | private string GetInnerText(XmlNode pXmlNode) |
| 87. | | | { |
| 88. | | | | if (pXmlNode != null) |
| 89. | | | | { |
| 90. | | | | | return pXmlNode.InnerText.Trim(); |
| 91. | | | | } |
| 92. | | | | return String.Empty; |
| 93. | | | } |
| 94. | | } |
| 95. | } |