使用asp.net渲染无序列表

前端之家收集整理的这篇文章主要介绍了使用asp.net渲染无序列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用asp.net/ C#渲染一个无序列表,其中包含从数据库获得的数据.我的数据看起来像这样.
  1. id Name Depth
  2.  
  3. 1 ELECTRONICS 0
  4.  
  5. 2 TELEVISIONS 1
  6.  
  7. 3 Tube 2
  8.  
  9. 4 LCD 2
  10.  
  11. 5 Plasma 2
  12.  
  13. 6 Portable electronics 1
  14.  
  15. 7 MP3 Player 2
  16.  
  17. 8 Flash 3
  18.  
  19. 9 CD Players 2
  20.  
  21. 10 2 Way Radio 2

使用上面的示例数据,我需要根据深度呈现无序列表,格式如下

  1. <ul>
  2. <li>ELECTRONICS
  3.  
  4. <ul>
  5.  
  6. <li>TELEVISIONS
  7.  
  8. <ul>
  9.  
  10. <li>TUBE</li>
  11.  
  12. <li>LCD</li>
  13.  
  14. <li>PLASMA</li>
  15.  
  16. </ul>
  17.  
  18. </li>
  19.  
  20. <li>PORTABLE ELECTRONICS
  21. <ul>
  22.  
  23. <li>MP3 PLAYERS
  24.  
  25. <ul>
  26.  
  27. <li>FLASH</li>
  28.  
  29. </ul>
  30.  
  31. </li>
  32.  
  33. <li>CD PLAYERS</li>
  34.  
  35. <li>2 WAY RAdioS</li>
  36.  
  37. </ul>
  38.  
  39. </li>
  40.  
  41. </ul>
  42.  
  43. </li>
  44.  
  45. </ul>

上面的数据只是一个示例,我有一个巨大的记录集,必须转换为无序列表.有人可以给我一个如何实现这一点的想法吗?

更新:
我更新了我的代码,生成无序列表到下面.

  1. int lastDepth = -1;
  2. int numUL = 0;
  3.  
  4. StringBuilder output = new StringBuilder();
  5.  
  6.  
  7. foreach (DataRow row in ds.Tables[0].Rows)
  8. {
  9.  
  10. int currentDepth = Convert.ToInt32(row["Depth"]);
  11.  
  12. if (lastDepth < currentDepth)
  13. {
  14. if (currentDepth == 0)
  15. {
  16. output.Append("<ul class=\"simpleTree\">");
  17. output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>",row["name"],row["id"]);
  18. }
  19. else
  20. {
  21. output.Append("<ul>");
  22. if(currentDepth==1)
  23. output.AppendFormat("<li><span>{0}</span>",row["name"]);
  24. else
  25. output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>",row["id"]);
  26. }
  27. numUL++;
  28. }
  29. else if (lastDepth > currentDepth)
  30. {
  31. output.Append("</li></ul></li>");
  32. if(currentDepth==1)
  33. output.AppendFormat("<li><span>{0}</span>",row["name"]);
  34. else
  35. output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>",row["id"]);
  36. numUL--;
  37. }
  38. else if (lastDepth > -1)
  39. {
  40. output.Append("</li>");
  41. output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>",row["id"]);
  42. }
  43.  
  44.  
  45. lastDepth = currentDepth;
  46. }
  47.  
  48. for (int i = 1; i <= numUL+1; i++)
  49. {
  50. output.Append("</li></ul>");
  51. }

使用上面的代码,我的无序列表看起来像这样.

  1. <ul class="simpleTree">
  2. <li class="root">
  3. <span><a href="#" title="root">root</a></span>
  4. <ul>
  5. <li class="open" >
  6. <span><a href="#" title=1>ELECTRONICS</a></span>
  7. <ul>
  8. <li>
  9. <span>TELEVISIONS</span>
  10. <ul>
  11. <li>
  12. <span class="text"><a href="#" title=3>TUBE</a></span>
  13. </li>
  14. <li>
  15. <span class="text"><a href="#" title=4>LCD</a></span>
  16. </li>
  17. <li><span class="text"><a href="#" title=5>PLASMA</a></span>
  18. </li>
  19. </ul>
  20. </li>
  21. <li>
  22. <span>PORTABLE ELECTRONICS</span>
  23. <ul>
  24. <li>
  25. <span class="text"><a href="#" title=7>MP3 PLAYERS</a></span>
  26. <ul>
  27. <li>
  28. <span class="text"><a href="#" title=8>FLASH</a></span>
  29. </li>
  30. </ul>
  31. </li>
  32. <li>
  33. <span class="text"><a href="#" title=9>CD PLAYERS</a></span>
  34. </li>
  35. <li>
  36. <span class="text"><a href="#" title=10>2 WAY RAdioS</a></span>
  37. </li>
  38. </ul>
  39. </li></ul>
  40. </li></ul>
  41. </li></ul>

谢谢.

解决方法

您可以在代码中执行类似的操作,然后将结果输出页面上的Literal控件:
  1. int lastDepth = -1;
  2. int numUL = 0;
  3.  
  4. StringBuilder output = new StringBuilder();
  5.  
  6.  
  7. foreach (DataRow row in yourDataTable.Rows) {
  8.  
  9. int currentDepth = row["Depth"];
  10.  
  11. if (lastDepth < currentDepth) {
  12. output.append("<ul>");
  13. numUL++
  14. }
  15. else if (lastDepth > currentDepth) {
  16. output.append("</li></ul></li>");
  17. numUL--
  18. }
  19. else if (lastDepth > -1) {
  20. output.append("</li>");
  21. }
  22.  
  23. output.appendformat("<li class=\"depth-{1}\">{0}",currentDepth);
  24.  
  25. lastDepth = currentDepth;
  26. }
  27.  
  28. for (int i = 1;i <= numUL;i++)
  29. {
  30. output.append("</li></ul>");
  31. }
  32.  
  33.  
  34.  
  35. yourLiteralControl.Text = output.toString();

更新:我这样做是为了在注释中请求的深度相关的列表项中放入一个css类.

猜你在找的asp.Net相关文章