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.
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top