如何设置html链接< a href>动态使用C# [英] How to set html link <a href> dynamically using C#

查看:272
本文介绍了如何设置html链接< a href>动态使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div> 
< ul>
< li>< a href =http:// localhost:52451 / Voronezh / Settings.aspx>设置< / a< / li>
< li>< a href =http:// localhost:52451 / LoginPages / Welcome.aspx>注销< / a< / li>
< / ul>
< / div>

我想在某些特殊情况下动态设置这些href链接。



我试过以下内容:

HTML代码

 < li>< a runat =serverid =toSettingsonserverclick =toSettings_ServerClick>设置< / a>< / li> 



代码

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

但它不起作用:我停留在同一页面上,而不是移动到设置页面。



哪里出错?应该改变什么?



预先感谢。

解决方案

更改 HRef 在这里没有多大作用 - 它改变了链接,它对页面没有任何直接影响。尝试 Response.Redirect ,我认为这是你正在寻找的。 I.e:

  // if语句中
Response.Redirect(Settings.aspx); //注意 - 这是本地路径


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.

I've tried the following:

HTML code

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

C# code

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. Try Response.Redirect, I think this is what you're looking for. I.e:

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

这篇关于如何设置html链接&lt; a href&gt;动态使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆