简体   繁体   中英

ASP.NET Hyperlink control not render href on ascx webpart

I have a Hyperlink control on an .ascx that looks like this:

<asp:HyperLink ID="hDocument" NavigateUrl="http://www.google.com" Text="Delegate Approval" Target="_blank" runat="server"></asp:HyperLink>

However, when I navigate to my SharePoint page where this URL is supposed to be displayed, the hyperlink is not clickable and there is no href in the HTML anchor tag () like so:

<a id="ctl00_ctl40_g_65ace0cb_fdf4_4d40_ae31_9736b2d39022_gvLevel1Approvals_ctl02_hDocument" target="_blank">Delegate Approval</a>

I place a normal HTML anchor under the Hyperlink control and that one works fine. I have no idea why the Hyperlink control doesn't produce a href attribute when it renders.

Edit:

Here's the original code:

<asp:HyperLink ID="hDocument" runat="server"></asp:HyperLink>

code behind

HyperLink hDocument = (HyperLink)e.Row.FindControl("hDocument");
hDocument.Text = "Delegate Approval";
hDocument.NavigateUrl = // builiding URL here;
hDocument.Target = "_blank";

我有同样的问题,只有在Code Behind (在Page_Load中 )中设置NavigateUrl才能解决。

So our situation with the was it would work only intermittently but 95% of the time it didn't work. Everything else worked fine: building the URL in the code behind, setting the link text, everything. We could not figure out why the href would not show up on the page on the client browser. In the end I eventually switched to using a regular HTML anchor tag and added in the System.Web.UI.HtmlControls namespace to find the anchor and modify that in the code behind.

I found the solution.

In VB.net

HyperLink1.Attributes.Add("href", "http://www.clarin.com")

In my situation Visual Studio converted ASPX to HTML5. When VS converts the document to HTML - if you perform some debugging - you will see:

<a NavigateURL="someurl" blablabla

but NavigateURL is not found in HTML.

You MUST replace the attribute NavigateURL with href .

I do not know if this is a compiler error, but it was the only reasonable solution I could find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM