Adding link to a label ASP.NET (VB)

半城伤御伤魂 提交于 2019-12-11 05:43:53

问题


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.


回答1:


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




回答2:


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.




回答3:


<a href="http://google.com" >Go to Google</a>



回答4:


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)


来源:https://stackoverflow.com/questions/3057533/adding-link-to-a-label-asp-net-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!