Nesting Literal Control in anchor tags


S

Spondishy

Im running the following html snippet in my page...

<a href='<asp:Literal id="litSubRegionCode"
runat="server"></asp:Literal>'>Test</a>

When I flick back to designer view I get the 'Could not re-format page'
message.

How do I correctly nest the literal inside the anchor tag...

Thanks.
 
Ad

Advertisements

R

recoil

Why are you trying to nest a literal control inside the "href"
property?
The HREF property is a string. If you need to set the href property
then consider making the <a tag a server control and set the property
via CodeBehind.
<a href="" runat="server" id="lnkTest">Test</a>

In your code behind there should be a variable declared as
protected HtmlAnchor lnkTest ;
If there is not then define it.
then set the property programmatically by going lnkTest.HRef = "....";
 
S

Spondishy

I want to use the literal control, not a htmlLink. I know I can use a
link, but I want to build a url and add my string in the middle. The
easiest way to do this is to use the literal control.

Anyone have an answer?

Thanks.
 
R

recoil

A Literal control cannot be put inside of an Attribute.
an ASPX follows basic HTMl/SGML based rules.
<root propery="<tag></tag>" is MALFORMED SGML and therefore is not
tolerated.

If you wish to use an existing value such as

<a href="/Dir/|/Action.aspx" runat="server" id="lnkTest">Test</a>

In your code behind there should be a variable declared as
protected HtmlAnchor lnkTest ;
If there is not then define it.
then set the property programmatically by going lnkTest.HRef = "....";

or since you said you wanted to "build a url and add my string in the
middle"
then do something like this
lnkTest.HRef = lnkTest.HRef.Replace("|", CurrentAction); // where
CurrentAction is some string that you want to dynamically insert or
maybe a variable from your web.config. etc
 
R

recoil

You may also be able to go <a href="Dir/<%= somevariable
%>/Action.aspx"></a> but having made the migration to asp.net along
time ago I would never do something like that so I am not sure as to
whether that is the exact syntax for asp.net or at what lifecycle stage
you would need to ensure somevariable has been set.
 
Ad

Advertisements

M

Matt Berther

Hello Spondishy,

Why not use <asp:Hyperlink id="myLink" runat="server">Test</asp:Hyperlink>

and in your code-behind, do myLink.NavigateUrl = "~/Action.aspx?subregion="
+ subRegionVariable
 
Ad

Advertisements


Top