Using HtmlAnchor or ASP.NET HyperLink for anchor tag that navigates in-page named anchor - asp.net

I am trying to render a simple hyperlink that links to a named anchor within the page, for example:
scroll to down
<a name="namedAnchor">down</a>
The problem is that when I use an ASP.NET control like asp:HyperLink or HtmlAnchor, the href="#namedAnchor" is rendered as href="controls/#namedAnchor" (where controls is the subdirectory where the user control containing the anchor is). Here is the code for the control, using two types of anchor controls, which both have the same problem:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="TestWebApplication1.controls.Test" %>
HtmlAnchor
<asp:HyperLink NavigateUrl="#namedAnchor" runat="server">HyperLink</asp:HyperLink>
The generated source looks like:
HtmlAnchor
HyperLink
I really just want:
HtmlAnchor
HyperLink
I am using the HtmlAnchor or HyperLink class because I want to make changes to other attributes in the code behind. I do not want to introduce a custom web control for this requirement, as the requirement I'm pursuing is not that important enough to justify abandoning the traditional ASP.NET link controls. It seems like I should be able to use the ASP.NET link controls to generate the desired link.

Instead of using the NavigateUrl property, just use the href property
<asp:HyperLink href="#namedAnchor" runat="server">HyperLink</asp:HyperLink>

To set the HREF property in codebehind:
HyperLink link = new HyperLink();
link.Attributes.Add("href", "#" + doc.DocumentID.ToString());
link.Text = doc.DocumentNumber;
This will give you:
blah blah

Set it as a custom property on the link:
HyperLink link = new HyperLink();
link.Attributes.Add("name", doc.DocumentID.ToString());
link.Text = doc.DocumentNumber;
This will give you:
<a name="111">blah blah</a>

If you must use NavigateUrl Property, which is sometimes necesary then you can use:
hypID.NavigateUrl = HttpContext.Current.Request.Url.AbsoluteUri & "#MyAnchor"

Related

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";
I had the same issue and could solve it only by setting the NavigateUrl in Code Behind (in Page_Load).
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.

Replace HTML with PlaceHolder control in ASP.NET

Is there any way to read some HTML and replace that HTML with a PlaceHolder control dynamically at runtime?
For example, I have some HTML that contains tags such as ##MainContent## or ##SideContent##.
I need to somehow find each tag and dynamically add a asp:PlaceHolder control.
EDIT - The reason for this is to read the HTML in and then dynamically add controls to a section of the page. I'm attempting to create a CMS system, although I'm not sure this is the best approach.
I hope this is a thought exercise. :)
Ideally, you should just replace those tags in your markup with an <asp:PlaceHolder> control. If you cannot do that (for whatever reason), try replacing them with an <asp:Literal> control:
<asp:Literal ID="litMainContent" runat="server" Text="##MainContent##"><asp:Literal>
<asp:Literal ID="litSideContent" runat="server" Text="##SideContent##"><asp:Literal>
That way, they will still render on the page exactly as they do now. Then you should be able to use litMainContent and litSideContent in your code-behind:
For VB.Net:
Dim phMainContent As PlaceHolder = New PlaceHolder
Dim phMainContent As PlaceHolder = New PlaceHolder
litMainContent.Controls.Add(phMainContent)
litSideContent.Controls.Add(phSideContent)
For C#.Net
PlaceHolder phMainContent = new PlaceHolder();
PlaceHolder phMainContent = new PlaceHolder();
litMainContent.Controls.Add(phMainContent);
litSideContent.Controls.Add(phSideContent);

Acessing Items on a Masterpage from a Child page

I have a <ASP:Label ID="lblDashboardLink" runat="server" /> located on a MasterPage named MasterBase.
From a page using this MasterPage, how can I access this label or any other items I need?
'VB.NET
'(From the child .aspx page)
Master.FindControl("lblDashboardLink"). <-- but don't see an option to change URL
I've been googling and I keep finding this same method, but it's focusing more on User Controls it looks like... Can anyone guide me in the right direction here?? I'm so used to MVC!
Master.FindControl("lblDashboardLink") always returns a Control (see MSDN). So all you have to do is cast it to Label. Then you may access any properties of a Label. Anyway there is no URL property in a label...
CType(Master.FindControl("lblDashboardLink"), Label).Text = "your value"
Try this:
CType(Master.FindControl("lblDashboardLink"), Label).Text = "some url"

How to set HtmlEditorExtender's content server-side

I'm using the AjaxControlToolkit's HtmlEditorExtender in my ASP.NET 4.0 web app:
<asp:TextBox ID="myTxt" runat="server" TextMode="MultiLine" Height="80px" Width="100%" />
<act:HtmlEditorExtender ID="heMyTxt" runat="server" TargetControlID="myTxt">
<Toolbar>
etc...
</Toolbar>
</act:HtmlEditorExtender>
When I set the content of the text box server-side like this:
myTxt.Text = htmlStringFromDatabase;
...the content in the textbox is the literal HTML markup (i.e. <b>Bold</b> shows up just like that, not like Bold). The formatting doesn't transfer, but the Extender does do its work on the textbox and set up its toolbar and buttons, etc. Is there a different way to set the content?
EDIT: turns out the HTML I get out of myTxt (the control that the extender is attached to) is encoded HTML. So now the question is how to stop the control from encoding its content. This problem is also presented in this question, but I'm not using LoadControl() or the designer to my page; I've written my markup manually.
Also, I don't know if this makes a difference, but I'm pulling the text out of the TextBox in the page's Page_Load handler.
Try to do like this,
myTxt.Text = HttpUtility.HtmlDecode(htmlStringFromDatabase);
I was able to solve this problem like this :
Literal lit = new Literal();
lit.Mode = LiteralMode.PassThrough;
lit.Text = HttpUtility.HtmlDecode(HTMLTExt);
TextBox1.Text = lit.Text; // The text box which HTMLEditorExtender is attached to

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));

Resources