|zyciis| 如何在aspx文件中写及时代码输出菜单事,谢谢 急

zyciis178 2010-04-09 08:00:00
如CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected List<Menu> MenuList = new List<Menu>();
protected void Page_Load(object sender, EventArgs e)
{
MenuList.Add(new Menu() { ID = 1, ParentID = 0, Name = "文件" });
MenuList.Add(new Menu() { ID = 2, ParentID = 0, Name = "编辑" });
MenuList.Add(new Menu() { ID = 3, ParentID = 1, Name = "新建" });
MenuList.Add(new Menu() { ID = 4, ParentID = 2, Name = "撤消" });
MenuList.Add(new Menu() { ID = 5, ParentID = 3, Name = "项目" });
MenuList.Add(new Menu() { ID = 6, ParentID = 1, Name = "打开" });
}
}
public class Menu
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
}

然后要在aspx中输出如下HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<ul>
<li>文件
<ul>
<li>新建
<ul>
<li>项目</li>
</ul>
</li>
<li>打开 </li>
</ul>
</li>
<li>编辑
<ul>
<li>撤消</li>
</ul>
</li>
</ul>
</body>
</html>

那么ASPX文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<%
for (int i = 0; i <= MenuList.Count - 1; i++)
{
}
//??这里应该如何来输出上面的那个菜单事呢?
//谢谢
%>
</body>
</html>
...全文
246 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2010-04-10
  • 打赏
  • 举报
回复
你如果想要

<ul>
<li>文件
<ul>
<li>新建
<ul>
<li>项目</li>
</ul>
</li>
<li>打开 </li>
</ul>
</li>
<li>编辑
<ul>
<li>撤消</li>
</ul>
</li>
</ul>

的格式

必须用代码来控制样式,单单靠循环是没有办法的


下面是不完全实现,但足以说明问题了


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<%
int parentID = 0;

list = GetList(parentID);

StringBuilder sb = new StringBuilder();

for (int i = 0; i <= list.Count - 1; i++)
{
if (i == 0)
{
sb.Append("<ul>");
}

sb.Append("<li>");

sb.Append(list[i].Name);

sb.Append("</li>");


if (i == list.Count - 1)
{
sb.Append("</ul>");
}
}

Response.Write(sb.ToString());
%>
</body>
</html>



protected List<Menu> MenuList = new List<Menu>(), list;


protected void Page_Load(object sender, EventArgs e)
{
MenuList.Add(new Menu() { ID = 1, ParentID = 0, Name = "文件" });
MenuList.Add(new Menu() { ID = 2, ParentID = 0, Name = "编辑" });
MenuList.Add(new Menu() { ID = 3, ParentID = 1, Name = "新建" });
MenuList.Add(new Menu() { ID = 4, ParentID = 2, Name = "撤消" });
MenuList.Add(new Menu() { ID = 5, ParentID = 3, Name = "项目" });
MenuList.Add(new Menu() { ID = 6, ParentID = 1, Name = "打开" });
}

protected List<Menu> GetList(int parentID)
{
List<Menu> list = new List<Menu>();

Recursion(list, parentID);

return list;
}



private void Recursion(List<Menu> list, int ID)
{
List<Menu> menus = MenuList.FindAll(menu => { return menu.ParentID == ID; });

for (int i = 0; i < menus.Count; i++)
{
list.Add(menus[i]);
Recursion(list, menus[i].ID);
}
}

zyciis178 2010-04-10
  • 打赏
  • 举报
回复
能否不要用拼接的方法,因为这样的话在CS中存在
sb.AppendFormat("<li>{0}{1}</li>", list[i].Name, Recursion(list[i].ID));
我不想在我的cs里面关联取html
我的cs只想提供数据,让aspx页面自己去生成他的html
如这种方法

<%
for (int i = 0; i <= RootList.Count - 1; i++)
{
%>
<li><%= RootList[i].Name %></li>
<%
}
%>

也就是说在CS中提供数据,aspx管理输出样式

谢谢
javascriptlover2 2010-04-10
  • 打赏
  • 举报
回复
你太敬业了
阿非 2010-04-10
  • 打赏
  • 举报
回复
ASP是怎么实现的
---------------

无限节点都需要递归 (二叉树有非递归实现)

而你需要设置不同项的样式 ,也就是在获得该项的同时 就需要指定样式

如果未指定,那之后是很难判断出该项该应用什么样式



ASP.NET MVC是怎么实现的

应该同上

阿非 2010-04-10
  • 打赏
  • 举报
回复
那能否把
private string Recursion(int ID)
这个方法写在页面中呢?
----------------
是可以的

只是不能使用 Lambda 表达式 和 FindAll 方法

需要自己写个方法实现此功能
zyciis178 2010-04-10
  • 打赏
  • 举报
回复
那能否把
private string Recursion(int ID)
这个方法写在页面中呢?

ASP是怎么实现的
ASP.NET MVC是怎么实现的

谢谢
---------------
PS:我现在想做的就是数据和页面分离,谢谢
阿非 2010-04-09
  • 打赏
  • 举报
回复

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<%
int parentID = 0;

RootList = GetList(parentID);

StringBuilder sb = new StringBuilder();

for (int i = 0; i <= RootList.Count - 1; i++)
{
if (i == 0)
{
sb.Append("<ul>");
}

sb.Append("<li>");

sb.Append(RootList[i].Name);

sb.Append(GetHTMLByID(RootList[i].ID));

sb.Append("</li>");


if (i == RootList.Count - 1)
{
sb.Append("</ul>");
}
}

Response.Write(sb.ToString());
%>
</body>
</html>


protected List<Menu> MenuList = new List<Menu>(), RootList;


protected void Page_Load(object sender, EventArgs e)
{
MenuList.Add(new Menu() { ID = 1, ParentID = 0, Name = "文件" });
MenuList.Add(new Menu() { ID = 2, ParentID = 0, Name = "编辑" });
MenuList.Add(new Menu() { ID = 3, ParentID = 1, Name = "新建" });
MenuList.Add(new Menu() { ID = 4, ParentID = 2, Name = "撤消" });
MenuList.Add(new Menu() { ID = 5, ParentID = 3, Name = "项目" });
MenuList.Add(new Menu() { ID = 6, ParentID = 1, Name = "打开" });
}

protected List<Menu> GetList(int parentID)
{
return MenuList.FindAll(menu => { return menu.ParentID == parentID; });
}



private string Recursion(int ID)
{

List<Menu> list = GetList(ID);


if (list.Count == 0)
{
return "";
}

StringBuilder sb = new StringBuilder();

for (int i = 0; i < list.Count; i++)
{
if (i == 0)
{
sb.Append("<ul>");
}

sb.AppendFormat("<li>{0}{1}</li>", list[i].Name, Recursion(list[i].ID));

if (i == list.Count - 1)
{
sb.Append("</ul>");
}
}

return sb.ToString();
}

protected string GetHTMLByID(int ID)
{
return Recursion(ID);
}


}

public class Menu
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
}


zyciis178 2010-04-09
  • 打赏
  • 举报
回复
RE:不能用Repeater
谢谢
直接在aspx中写及时代码出来,像asp的写法,谢谢
阿非 2010-04-09
  • 打赏
  • 举报
回复
repeater 可以用 循环来替换

repeater在上面的例子里就是 起到遍历的效果
阿非 2010-04-09
  • 打赏
  • 举报
回复
你之前问过吧

我记得我还写过代码
zyciis178 2010-04-09
  • 打赏
  • 举报
回复
RE:
构建字符串
或使用repeater在模板列输出数据
-------------------------------
我现在是要在aspx里面输出
repeater不能用,我不用服务器控件

你的就当作现在是在ASP.NET MVC中输出

谢谢
wuyq11 2010-04-09
  • 打赏
  • 举报
回复
构建字符串
StringBuilder sb=new StringBuilder();
for (int i = 0; i <= MenuList.Count - 1; i++)
{

}
根据ParentID 查询子集
或使用repeater在模板列输出数据

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧