假设:
<div class="subMenu">
<a href="products.aspx" class="subLink">Products</a> <a href="productCats.aspx" class="subLink">Categories</a> <a href="example.aspx" class="subLink">Another</a>
</div>
在我的代码背后,我将如何以编程方式更改该div中的链接列表?
在我的代码背后,我会有以下几点:
if(menuID == 0) {
// Display some links here
}else if(menuID == 1) {
// Display some links here
}else if(menuID == 2) {
// Display some links here
}
抱歉简单,只是想学习。
答案 0 :(得分:2)
稍微复杂的方式:
<asp:repeater id="rptMenu" runat="server">
<itemTemplate>
<a href='<%# Eval("LinkUrl") %>' class="subLink"><%# Eval("LinkName") %></a>
<itemTemplate>
</asp:repeater>
然后在后端建立一个链接列表并将它们绑定到转发器。
public class MenuItem
{
public string LinkUrl { get; set; }
public string LinkName { get; set; }
}
public void Page_Load()
{
//GetMenuItems would populate this list, depending on your logic
List<MenuItem> menuItems = GetMenuItems(menuId);
rptMenu.DataSource = menuItems;
rptMenu.DataBind()
}
这样做的好处是你可以从数据库中驱动它,如果需要,你可以轻松编辑输出。
答案 1 :(得分:1)
我认为你应该使用Panel控件并在里面添加超链接。
ASPX页面:
<asp:Panel ID="TestPanel" CssClass="Submenu" runat="server">
</asp:Panel>
代码背后的代码:
switch (menuId)
{
case 0:
TestPanel.Controls.Add(new HyperLink { Text = "Test", NavigateUrl = "testUrl", CssClass="Sublink" });
TestPanel.Controls.Add(new HyperLink { Text = "Test2", NavigateUrl = "testUrl2", CssClass = "Sublink" });
break;
case 1:
TestPanel.Controls.Add(new HyperLink { Text = "xxx", NavigateUrl = "xxx", CssClass="Sublink" });
TestPanel.Controls.Add(new HyperLink { Text = "xxx", NavigateUrl = "xxx", CssClass = "Sublink" });
break;
case 2:
TestPanel.Controls.Add(new HyperLink { Text = "xxx", NavigateUrl = "xxx", CssClass = "Sublink" });
TestPanel.Controls.Add(new HyperLink { Text = "xxx", NavigateUrl = "xxx", CssClass = "Sublink" });
break;
default:
break;
}
答案 2 :(得分:0)
一种简单的方法:
<div class="subMenu">
<%= myLinks %>
</div>
if(menuID == 0) {
myLinks = "<a href='products.aspx' class='sublink'>Products</a>etc.. etc.. "
}else if(menuID == 1) {
myLinks = "<a href='products.aspx' class='sublink'>Products</a>etc.. etc.. "
}else if(menuID == 2) {
myLinks = "<a href='products.aspx' class='sublink'>Products</a>etc.. etc.. "
}
添加:
Protected myLinks as string = ""
低于班级定义。
答案 3 :(得分:0)
我不知道这是不是最好的方式 - 但它是一种方式:)
[Serializable]
public class MyLinks
{
public string cLink;
public string cTitle;
}
public partial class Dokimes_StackOverFlow_AplesDokimes : System.Web.UI.Page
{
MyLinks [] MyLinksAre = {
new MyLinks{cLink = "products.aspx", cTitle = "products"},
new MyLinks{cLink = "ProductCat.aspx", cTitle = "catalog"},
new MyLinks{cLink = "Paradeigma.aspx", cTitle = "example"},
};
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sRenderOnMe = new StringBuilder();
int menuID = 0;
switch (menuID)
{
case 0:
foreach (int i in new int[] { 0, 1 })
sRenderOnMe.AppendFormat("<a href=\"{0}\">{1}</a>", MyLinksAre[i].cLink, MyLinksAre[i].cTitle);
break;
default:
foreach (int i in new int[] { 0, 1, 2 })
sRenderOnMe.AppendFormat("<a href=\"{0}\">{1}</a>", MyLinksAre[i].cLink, MyLinksAre[i].cTitle);
break;
}
txtMenouRender.Text = sRenderOnMe.ToString();
}
}
在页面上
<asp:Literal runat="server" ID="txtMenouRender"></asp:Literal>
答案 4 :(得分:0)
你想要一个简单的href标签或一些asp.net控件吗?
<div class="subMenu">
<% if(menuID == 0) { %>
<a href="products.aspx" class="subLink">Products</a>
<% } else if(menuID == 1) { %>
<a href="productCats.aspx" class="subLink">Categories</a>
<% } else { %>
<a href="example.aspx" class="subLink">Another</a>
<% } %>
</div>
您还可以添加控件:
<div class="subMenu">
<% if(menuID == 0) { %>
<asp:LinkButton ID="Test" runat="server" OnClick="Test_OnClick" class="subLink">Products</LinkButton>
<% } else if(menuID == 1) { %>
<a href="productCats.aspx" class="subLink">Categories</a>
<% } else { %>
<a href="example.aspx" class="subLink">Another</a>
<% } %>
</div>