2015年7月27日 星期一

ASP.NET jQuery and Database Driven Accordion

參考引用來源:ASP.NET jQuery and Database Driven Accordion
請參考來源:Building a Database Driven Hierarchical Menu using ASP.NET and SooperFish Jquery Plugin

--
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
  4. <style type="text/css">
  5. .Menu
  6. {
  7. width: 200px;
  8. text-align: center;
  9. border: solid 2px gray;
  10. padding: 0px;
  11. background-color: Silver;
  12. cursor: hand;
  13. font-weight: bold;
  14. }
  15. .MenuItem
  16. {
  17. width: 192px;
  18. text-align: center;
  19. border: solid 1px silver;
  20. padding: 2px;
  21. background-color: whitesmoke;
  22. }
  23. </style>
  24. <script type="text/javascript">
  25. $(document).ready(function() {
  26. $.ajax(
  27. {
  28. type: "POST",
  29. url: "ASPNET jQuery and Database Driven Accordion.aspx/GetMenus",
  30. contentType: "application/json; charset=utf-8",
  31. dataType: "json",
  32. success: CreateMenus,
  33. error: function(err) {
  34. alert(err.status + " - " + err.statusText);
  35. }
  36. });
  37. });
  38. function CreateMenus(results) {
  39. results = results.d;
  40. for (var i = 0; i < results.length; i++) {
  41. $("<div class='Menu'>" + results[i].Text + "</div>")
  42. .click({ MenuId: results[i].MenuId }, OnMenuClick)
  43. .appendTo("#accordionContainer");
  44. }
  45. }
  46. function OnMenuClick(event) {
  47. $("div[id ^= 'menuItemGroup']").slideUp(500);
  48. $.ajax(
  49. {
  50. type: "POST",
  51. url: "ASPNET jQuery and Database Driven Accordion.aspx/GetMenuItems",
  52. data: '{"menuId":"' + event.data.MenuId + '"}',
  53. contentType: "application/json; charset=utf-8",
  54. dataType: "json",
  55. success: function(items) {
  56. items = items.d;
  57. $(event.target).children().remove();
  58. var html = "<div id='menuItemGroup" + event.data.MenuId + "' style='display:none'>";
  59. for (var j = 0; j < items.length; j++) {
  60. html += "<div class='MenuItem'><a href='" + items[j].NavigateUrl + "'>" +
  61. items[j].Text + "</a></div>";
  62. }
  63. html += "</div>";
  64. $(event.target).append(html);
  65. $("#menuItemGroup" + event.data.MenuId).slideDown(500);
  66. },
  67. error: function(err, r, c) {
  68. alert(err.status + " - " + err.statusText);
  69. }
  70. }
  71. )
  72. }
  73. </script>
  74. </head>
  75. <body>
  76. <form id="form1" runat="server">
  77. <div id="accordionContainer">
  78. </div>
  79. </form>
  80. </body>
  81. </html>
---
  1. public class Menu
  2. {
  3. public int MenuId { get; set; }
  4. public string Text { get; set; }
  5. }
  6. public class MenuItem
  7. {
  8. public int MenuId { get; set; }
  9. public int MenuItemId { get; set; }
  10. public string Text { get; set; }
  11. public string NavigateUrl { get; set; }
  12. }
  13. public partial class ASPNET_jQuery_and_Database_Driven_Accordion : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. }
  18. [System.Web.Services.WebMethod]
  19. public static System.Collections.Generic.List<Menu> GetMenus()
  20. {
  21. System.Collections.Generic.List<Menu> list = new System.Collections.Generic.List<Menu>();
  22. for (int i = 0; i < 6; i++)
  23. {
  24. list.Add(new Menu() {
  25. MenuId= i,
  26. Text = "Menu text "+i.ToString()
  27. });
  28. }
  29. return list;
  30. }
  31. [System.Web.Services.WebMethod]
  32. public static System.Collections.Generic.List<MenuItem> GetMenuItems(int menuId)
  33. {
  34. Random r = new Random();
  35. System.Collections.Generic.List<MenuItem> list = new System.Collections.Generic.List<MenuItem>();
  36. for (int i = 0; i < r.Next(10); i++)
  37. {
  38. MenuItem mi = new MenuItem();
  39. mi.MenuId = menuId;
  40. mi.MenuItemId = i;
  41. mi.Text = "Menu "+ menuId.ToString()+" --- Menu item "+ i.ToString();
  42. mi.NavigateUrl ="http://www.testmenu.com?menuID="+menuId.ToString()+"&MenuItemID="+i.ToString() ;
  43. list.Add(mi);
  44. }
  45. return list;
  46. }
  47. }

沒有留言:

張貼留言