我已经习惯了VB和ASP经典,我现在正在尝试在ASP.net上写我的第一个网站,我正在学习,因为我去了...但我今天花了很多时间尝试做这个,我没有通常的谷歌搜索。
希望在我的代码中显而易见的是,我尝试使用静态ID在LinkButton下面动态添加2个LinkButtons和唯一ID。我希望2个唯一的LinkButtons中的ID在其中包含“companyname”dataitem值,以便在单击第一个“companylink”时取消隐藏它们 - 我还没有编写子“LoadCompany”来取消隐藏两个LinkButton的但我很自信我已经知道该怎么做了。
<asp:Content ContentPlaceHolderID="leftsidebarcontent" runat="server">
<li id="leftsidebarhead" class="sidebaritems">/* MENU */</li><br />
<asp:Datalist id="customers" runat="server" RepeatLayout="Flow">
<ItemTemplate>
<li class="leftsidebaritem sidebaritems"><asp:LinkButton id="CompanyLink" runat="server" OnCommand="LoadCompany" Text='<%#Container.DataItem("companyname")%>'></asp:LinkButton></li>
<li class="leftsidebaritem sidebaritems"><asp:LinkButton id='<%#Container.DataItem("companyname")%> & "Reviews"' runat="server" Text="Reviews"></asp:LinkButton></li>
<li class="leftsidebaritem sidebaritems"><asp:LinkButton id='LinkButton1' runat="server" Text="Other"></asp:LinkButton></li>
<br />
</ItemTemplate>
</asp:Datalist>
如果有帮助 - 第一个LinkButton是填充页面左侧的导航菜单,在这些LinkButton的每一个下面我想要隐藏另外两个链接按钮,直到点击原始链接按钮,此时它们取消隐藏 - 所以创建一种嵌套的导航菜单。
答案 0 :(得分:0)
你可以在标记中这样做:
<asp:Content ID="Content1" ContentPlaceHolderID="leftsidebarcontent" runat="server">
<p id="leftsidebarhead" class="sidebaritems">/* MENU */</p>
<br />
<asp:DataList ID="customers" runat="server" RepeatLayout="Flow" OnItemCommand="customers_ItemCommand" >
<ItemTemplate>
<li class="leftsidebaritem sidebaritems">
<asp:LinkButton ID="CompanyLink" runat="server" CommandName="LoadCompany"
Text='<%#DataBinder.Eval(Container.DataItem, "companyname")%>'></asp:LinkButton></li>
<ul id="ulSubItems" runat="server" visible="false">
<li class="leftsidebaritem sidebaritems">
<asp:LinkButton ID='LinkButton1' runat="server" Text="Reviews"></asp:LinkButton></li>
<li class="leftsidebaritem sidebaritems">
<asp:LinkButton ID='LinkButton2' runat="server" Text="Other"></asp:LinkButton></li>
</ul>
<br />
</ItemTemplate>
</asp:DataList>
</asp:Content>
在代码隐藏中:
Protected Sub customers_ItemCommand(source As Object, e As DataListCommandEventArgs) Handles customers.ItemCommand
'Hide all subitems
For Each itm As DataListItem In customers.Items
If itm.ItemType = ListItemType.Item OrElse itm.ItemType = ListItemType.AlternatingItem Then
Dim ulSubItems As HtmlGenericControl = CType(itm.FindControl("ulSubItems"), HtmlGenericControl)
ulSubItems.Visible = False
End If
Next
If e.CommandName = "LoadCompany" Then
Dim MySubItems As HtmlGenericControl = CType(e.Item.FindControl("ulSubItems"), HtmlGenericControl)
If Not MySubItems Is Nothing Then
MySubItems.Visible = True
End If
End If
End Sub