简体   繁体   English

如何<a href>使用C#动态</a>设置HTML链接

[英]How to set html link <a href> dynamically using C#

I have a menu in my web application like this 我的网络应用程序中有一个这样的菜单

<div>
  <ul>
    <li><a href="http://localhost:52451/Voronezh/Settings.aspx">Settings</a</li>
    <li><a href="http://localhost:52451/LoginPages/Welcome.aspx">Log out</a</li>
  </ul>
</div>

I would like to set these href links dynamically if some special condition is true. 如果某些特殊条件成立,我想动态设置这些href链接。

I've tried the following: 我尝试了以下方法:

HTML code HTML代码

<li><a runat="server" id="toSettings" onserverclick="toSettings_ServerClick">Settings</a></li>

C# code C#代码

protected void toSettings_ServerClick(object sender, EventArgs e)
    {
       if (condition)
         toSettings.HRef = "http://localhost:52451/Voronezh/Settings.aspx"; 
       else
         {...}      
    }

but it doesn't work: I stay on the same page instead of moving to the Settings page. 但它不起作用:我停留在同一页面上,而不是移至“设置”页面。

Where is a mistake? 哪里有错? What should be changed? 应该改变什么?

Thanks in advance. 提前致谢。

Changing the HRef won't do much here - it changes the link, it doesn't have any direct effect on the page. 更改HRef在这里没有多大作用-它更改了链接,对页面没有任何直接影响。 Try Response.Redirect , I think this is what you're looking for. 试试Response.Redirect ,我想这就是您要寻找的。 Ie: 即:

// inside the if statement
Response.Redirect("Settings.aspx"); // note - this is the local path

Assuming this is ASP.NET C# 假设这是ASP.NET C#

Redirect method from the HttpResponse should be what you are looking for. 从HttpResponse重定向的方法应该是您想要的。

Reference: 参考:

https://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/a8wa7sdt(v=vs.110).aspx

If you want to build your menu dynamically in code behind you should probably use a <asp:Literal ID="menu" runat="server"/> tag in ASPX and fill it in the Page_Load event like 如果要在后面的代码中动态构建菜单,则可能应在ASPX中使用<asp:Literal ID="menu" runat="server"/>标记,并将其填充到Page_Load事件中,例如

if (!Page.IsPostBack) {
    menu.Text = GenerateMenu();
}

private string GenerateMenu() {
    if (yourcondition) {
        return "<div><ul><li><a href="...."></a></li></ul></div>"; 
    } else {

    }
}

Another alternative to doing a postback, executing the logic, and then doing a redirect would be to change the link tag to the hyperlink control, execute the logic on page load, and set the properties dynamically. 进行回发,执行逻辑然后进行重定向的另一种方法是将链接标记更改为超链接控件,在页面加载时执行逻辑,并动态设置属性。

HTML: HTML:

<asp:HyperLink id="toSettings" Text="Settings" runat="server"/> 

Code Behind 背后的代码

protected void Page_Load(object sender, EventArgs e) {
  toSettings.NavigateUrl = "http://localhost:52451/Voronezh/Settings.aspx";
}

This way you could even change the text (use toSettings.Text = "Text to display"; ) or the visibility (use toSettings.Visible = false; ) if needed. 这样,您甚至可以根据需要更改文本(使用toSettings.Text = "Text to display"; )或可见性(使用toSettings.Visible = false; )。

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

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