添加链接到标签 ASP.NET (VB)
我有一个标签
,我想向其添加一个链接。
我想使用 javascript
,例如:
MyLabel.Attributes.Add("`onclick`", "javascript:`SOME_CODE`")
我必须在 (SOME_CODE
) 中添加什么才能将用户重定向到另一个链接。
谢谢。
I have a label
and I want to add to it a link.
I want to use javascript
like :
MyLabel.Attributes.Add("`onclick`", "javascript:`SOME_CODE`")
What must I add in (SOME_CODE
) to redirect the user to another link.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这与您的上一个问题有关,请使用
Hyperlink
控件而不是标签
:If this has anything to do with your previous question, use a
Hyperlink
control instead of aLabel
:您是否尝试过:
window.location = 'http://google.com'
?您想要使用 Javascript 来实现此目的是否有任何特殊原因,而不仅仅是 HyperLink 控件?
更新:
您可以使用普通的 a 标记
link
或使用 ASP .Net HyperLink 控件:这是标记:
这是如果您想从代码隐藏中添加它:
其中
parentControl
是您想要将其添加到的容器,例如 a 中的单元格桌子或面板。有关如何向面板添加控件的详细信息,请参阅此处
Have you tried:
window.location = 'http://google.com'
?Are the any particular reason you want to use Javascript for this, and not just the HyperLink Control?
Update:
You can either use a normal a-tag
<a href="http://google.com">link</a>
or use the ASP.Net HyperLink control:This is the markup:
This is if you want to add it from the code-behind:
Where
parentControl
, is the container you want to add it to, for instance a cell in a table or a panel.See here for more information on how to add a control to a panel
只需使用普通的锚标记 (
),但将标签放在锚内部(反之不是严格有效的 html)。如果您不希望它每次都显示为链接,可以通过省略
href
属性来实现。使用普通的
服务器控件很容易做到这一点,如下所示:现在,只有在代码中实际设置 NavigateUrl 属性时,才会呈现 href 属性。您可能还会发现使用
完全取代了对标签的需要。Just use a plain anchor tag (
<a >
), but put the label inside the anchor (the reverse is not strictly valid html). If you don't want it to show up as a link every time, you can accomplish that by omitting thehref
attribute. This is easy to do with a normal<asp:HyperLink>
server control like so:Now, the href attribute will only render if you actually set the NavigateUrl property in your code. You might also find that using an
<asp:HyperLink>
completely replaces the need for the label.