开发者

ASP.net best way to change links

Given:

<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>

In my code behind, how would I programatically change the list of links in that div?

In my code behind I would have something along the lines of:

if(menuID == 0) {
  // Display some links here
}else if(menuID == 1) {
  // Display some links here
}else if(menuID == 2) {
  //开发者_StackOverflow中文版 Display some links here
}

Sorry for the simplicity, just trying to learn.


A slightly more complex way:

<asp:repeater id="rptMenu" runat="server">
   <itemTemplate>
     <a href='<%# Eval("LinkUrl") %>' class="subLink"><%# Eval("LinkName") %></a>
   <itemTemplate>
</asp:repeater>

And then build up a list of links in the back end and bind them to the 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()
}

This has the advantage that you could maybe drive this from a database, and you can easily edit the output if you need to.


I think you should use Panel control and add Hyperlinks inside.

ASPX page:

<asp:Panel ID="TestPanel" CssClass="Submenu" runat="server">
</asp:Panel>

code behind:

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;
    }


I do not know if this is the best way - but it a way :)

[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();
    }
}

On the page

<asp:Literal runat="server" ID="txtMenouRender"></asp:Literal>


Do you want a simple href tag or some asp.net controls?

<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>

You can put also controls:

<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>


A simple way:

    <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.. "
}

Add:

Protected myLinks as string = ""

below the class definition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜