如何设置html链接< a href>动态使用C# [英] How to set html link <a href> dynamically using 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链接< a href>动态使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!