添加链接到标签 ASP.NET (VB)

发布于 2024-09-05 14:56:51 字数 239 浏览 2 评论 0原文

我有一个标签,我想向其添加一个链接。

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝影如岚 2024-09-12 14:56:53

如果这与您的上一个问题有关,请使用Hyperlink控件而不是标签

    Dim Hyperlink1 As New Hyperlink
    Hyperlink1.Text = "XYZ"
    Hyperlink1.NavigateUrl = "http://www.google.com"

    Dim Literal1 As New Literal
    Literal1.Text = "<br />"

    ' Add the control to the placeholder
    PlaceHolder1.Controls.Add(Hyperlink1)
    PlaceHolder1.Controls.Add(Literal1)

If this has anything to do with your previous question, use a Hyperlink control instead of a Label:

    Dim Hyperlink1 As New Hyperlink
    Hyperlink1.Text = "XYZ"
    Hyperlink1.NavigateUrl = "http://www.google.com"

    Dim Literal1 As New Literal
    Literal1.Text = "<br />"

    ' Add the control to the placeholder
    PlaceHolder1.Controls.Add(Hyperlink1)
    PlaceHolder1.Controls.Add(Literal1)
烂柯人 2024-09-12 14:56:52

您是否尝试过:window.location = 'http://google.com'
您想要使用 Javascript 来实现此目的是否有任何特殊原因,而不仅仅是 HyperLink 控件?

更新:

您可以使用普通的 a 标记link 或使用 ASP .Net HyperLink 控件:

这是标记:

<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="http://google.com" runat="server" />

这是如果您想从代码隐藏中添加它:

HyperLink link = new HyperLink();
link.NavigateUrl = "http://google.com";

parentControl.Controls.Add(link);

其中 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:

<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="http://google.com" runat="server" />

This is if you want to add it from the code-behind:

HyperLink link = new HyperLink();
link.NavigateUrl = "http://google.com";

parentControl.Controls.Add(link);

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

千纸鹤 2024-09-12 14:56:52

只需使用普通的锚标记 (),但将标签放在锚内部(反之不是严格有效的 html)。如果您不希望它每次都显示为链接,可以通过省略 href 属性来实现。使用普通的 服务器控件很容易做到这一点,如下所示:

<asp:HyperLink id="..." runat="server"><asp:Label ... ></asp:Label></asp:HyperLink>

现在,只有在代码中实际设置 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 the href attribute. This is easy to do with a normal <asp:HyperLink> server control like so:

<asp:HyperLink id="..." runat="server"><asp:Label ... ></asp:Label></asp:HyperLink>

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.

怪我太投入 2024-09-12 14:56:52
<a href="http://google.com" >Go to Google</a>
<a href="http://google.com" >Go to Google</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文