Why does using <%=ConfigurationManager.AppSettings("MySetting")%> cause href attribute not to render in asp:HyperLink? - asp.net

I am trying to bind a HyperLink control's NavigateUrl property in the markup using a server tag like so:
<asp:HyperLink ID="lnkHelp" runat="server" NavigateUrl='<%#ConfigurationManager.AppSettings("HelpUrl")%>'>Text</asp:HyperLink>
The IDE recognizes it and I even get intellisense, but the tag ends up rendering without the href attribute. I've discovered <%$ AppSettings:HelpUrl%> and I have started using this, but I don't get intellisense with it. That's not a deal breaker, but intellisense is just nice. That's a question for another time, though, so I am mainly just wanting to know why using <%# %> causes the href attribute not to render.

You should use like this
NavigateUrl='<%$ ConfigurationSettings.AppSettings["HelpUrl"] %>'

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.

ASP.Net Placeholder vs if directive

When working with markup if I want to include some content conditionally, I use a placeholder in a normal way:
<asp:Placeholder Visible=<%# IsExpired %>
<span>Prolong your subscription</span>
</asp:PlaceHolder>
Also I can use if-directive:
<% if(IsExpired) {%>
<span>Prolong your subscription</span>
<% }%>
I prefer using the first one just because it does not make my markup messy. And what's the best way to conditionally include content? From the performance point of view, are they similar?
native HTML tags are always faster than rendering server controls as there is no time spent for rendering them
I think there is hardly anything to do with performance whether way you choose here. But actually you may use the following code:
<asp:Label runat="server" Visible=<%# IsExpired %>
Prolong your subscription</asp:Label>
instead the other two. This could make it look more straight-forward.
I'd never use C# code in Web Forms view. In addition I will avoid setting the Visible property in the markup and I will set it in the code behind on some event.
phWhatever.Visible = IsExpired;
Quite often you can avoid creating the IsExpired property.
Of course what #Johnny suggested is correct. If you need to hide what is effectively only one control you hide the control directly.

<script> tags inside an <asp:repeater>

I'm outputting a few lines of Javascript within a Repeater control on an ASPX page. I want to use a value from my DataSource inside the script tag.
A very basic example might be:
<asp:Repeater ID="RepeaterBlah" runat="server">
<ItemTemplate>
Hello <%# DataBinder.Eval(Container.DataItem, "SomeName")%>
<script>myfunction(<%# DataBinder.Eval(Container.DataItem, "SomeNumber")%>)</script>
</ItemTemplate>
</asp:Repeater>
I'm aware that most people won't repeat script tags like this, but I am using a small snippet of code from a third-party that you can place anywhere on a page to create a Flash object. You pass it a number so it knows which image gallery to display. No problems using several on one page.
To begin with, this worked fine, although I noticed the colours in Visual Web Developer indicated that it didn't really like the <%# being used inside a <script> tag. Intellisense was going a bit nuts in the code-behind too!
So what is the correct way to pass Dataset items into a script tag?
This perhaps? (Can't quite remember if the + signs should in fact be & signs though)
<%# "<script>myfunction(" + DataBinder.Eval(Container.DataItem, "SomeNumber") + ")</script>" %>
Another alternative syntax would be the following:
<%# Eval("SomeNumber", "<script>myfunction({0});</script>") %>
This uses the optional parameter where you can supply a format string.

ASP.NET Code Expression, Data Binding, and other Declarative Expressoins

What are the differences in these tags?
<%
<%#
<%=
<%$
More importantly, how do I display a page property using declarative syntax in an ASP.NET control? I'm trying to do this in an ASP.NET control. The task is to set the text of a label but I do not want to do this pro grammatically in the event I want to change the output control. I get an error about server side controls can't contain this syntax. I'm not sure that I need a databound control for what I want to do but that is another option.
Partial answer coming up.
Update
There is a new tag I've seen in ASP.NET 4.5? site
<%:
Partial answer
quoted from Mike Banavige
<% %> An embedded code block is
server code that executes during the
page's render phase. The code in the
block can execute programming
statements and call functions in the
current page class.
http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying
single pieces of information.
http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax.
http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression.
http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%# %> Directive Syntax.
http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments.
http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
Update:
Okay this appears to work
<asp:Label ID="MyLabel" runat="server" Text='<%# MyProperty%>'></asp:Label>
If I use the eval syntax then I get an error about databound control or I use the <% then I get a server side controls error. Any more color appreciated.. not sure I really understand what is going on.
Perhaps it has something to do with the render phase.
Few more observations:
I can use <%= without databinding and get the property value but can not use it in a server side control without getting error.
If I use <%# in server side control but I'm required to do a Page.Databind.
Interestingly, I can use either <%= or <%# when I want to render text that is not inside a control. Although the latter requires databinding.
The new <%: syntax is explained, also called code expression syntax
With ASP.NET 4 we are introducing a new code expression syntax (<%:
%>) that renders output like <%= %> blocks do – but which also
automatically HTML encodes it before doing so.
http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2
No, server-side controls can't. For example, I have a string property named SkinPath that give me the full App_Themes path to the current theme. I use it in the following way:
<img src='<%= SkinPath %>/Images/myImage.png' />
However, the following doesn't work:
<asp:Image ID='image' runat='server' ImageUrl='<%= SkinPath %>/Images/myImage.png' />
Instead, it renders the src literally in the result <img>.

Binding Label to SiteMap Current Node

What I want to do is something like this:
<asp:Label ID="titleLabel" runat="server"
**Text='<%# SiteMap.CurrentNode.Title %>'**></asp:Label>
Where I can bind the name of the current page node in the Site Map to the title label on that page. We are doing this because, until we get these names finalized, they may change often. The above code does not work, at least for me; it displays nothing.
Any ideas are appreciated.
EDIT: Obviously I could do this in the code behind (i.e. Page Load event or something similar) but I would really rather do it in the aspx code.
It does work with
<span><%= SiteMap.CurrentNode.Title %></span>
which is the same output as asp:Label
As an alternative to using a label, you could also use the SiteMapPath control and hide the parent nodes:
<asp:SiteMapPath ID="SiteMapPath1" runat="server" ParentLevelsDisplayed="0">
The property ParentLevelsDisplayed allows you to specify how many parent nodes of the current sitemap node you want to display.
Its been a while but I believe its <%= #Eval(SiteMap.CurrentNode.Title) %>
Edit:
Text='<%= SiteMap.CurrentNode.Title%>'
Hopefully that works the same as it would <%= SiteMap.CurrentNode.Title%>.

Resources