简体   繁体   English

ASP.NET包装了一个asp:HyperLink <li> 从服务器端来的

[英]ASP.NET wrap an asp:HyperLink in <li>'s, from server-side

I have the next structure for a menu item: 我有一个菜单项的下一个结构:

class MenuItem
{
    public string Path, Title;
}

I want to be able to Iterate an object of MenuItem[] , creating a new object of asp:HyperLink on each iteration, and to add it to a <ul> list. 我希望能够迭代MenuItem[]对象,在每次迭代时创建一个新的asp:HyperLink对象,并将其添加到<ul>列表中。

One thing that must happen is that each HyperLink will be inside a <li> tag. 必须发生的一件事是每个HyperLink都在<li>标签内。

How can I do that? 我怎样才能做到这一点?

You can use a repeater. 您可以使用转发器。 In the aspx: 在aspx中:

<asp:Repeater ID="repMenuItems" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li><asp:HyperLink ID="lnkMenuItem" runat="server" Text='<%# Eval("Title") %>' NavigateUrl='<%# Eval("Path")%>'/></li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

And in the codebehind: 在代码隐藏中:

repMenuItems.DataSource = arrMenuItem;  // your MenuItem array
repMenuItems.DataBind();

Additionaly you should change your class code for using Public Properties instead of Public Members, like this: 另外,您应该更改使用公共属性而不是公共成员的类代码,如下所示:

public class MenuItem 
{ 
    public string Title {get;set;} 
    public string Path {get;set;} 
}

I recommend you to read more about Properties in .NET, a nice feature for object encapsulation http://msdn.microsoft.com/en-us/library/65zdfbdt(v=vs.71).aspx 我建议你阅读更多关于.NET中的属性,这是一个很好的对象封装功能http://msdn.microsoft.com/en-us/library/65zdfbdt(v=vs.71).aspx

Hope this helps you 希望这对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM