Solved

Setting href of HtmlAnchor control (not in code-behind file)

Posted on 2004-10-04
8
793 Views
Last Modified: 2009-07-29
I had the following regular html anchor tag defined in my user control.

<a href="<%=Request.ApplicationPath%>/Default.aspx">Home</a>

This works fine. Request.ApplicationPath evaluates and all is good.

However, if I define it as runat="server" like this:

<a id="HomeANC" runat="server" href="<%=Request.ApplicationPath%>/Default.aspx">Home</a>

This results in the link pointing to "http://localhost/mywebapp/myusercontrolsfolder/<%=Request.ApplicationPath%>/Default.aspx".
Request.ApplicationPath no longer gets evaluated properly.

How the heck do I set the href without setting it in the Page_Load event handler?

0
Comment
Question by:mppeters
  • 4
  • 2
8 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12216524
As far as I know that server tags cannot contain <% ... %> constructs.

-tushar
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12216624
You can add a script block like following to set property of HRef dynamically..

----------------------------------------------------------------------
<script runat="server">
      public void Page_Load()
      {
            HomeANC.HRef= Request.ApplicationPath + "/Default.aspx";
      }
</script>
----------------------------------------------------------------------
(this code is cAsE sensitive)

-tushar
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12216703
You can read more on Inline Code here:

http://www.sitepoint.com/article/asp-dot-net-basics

-tushar
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 8

Author Comment

by:mppeters
ID: 12217730
>> As far as I know that server tags cannot contain <% ... %> constructs.

Sure they can.
Example:
<asp:Image Runat="server" Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsActive")) %>' ImageUrl="_images/check.gif" ID="ActiveIMG" />

It seems that the HtmlAnchor control does not support inline code. Though a regular html <a> tag without runat="server" works fine. Seems like a bug or an oversight. The only usecase supported is setting the Href attribute through non-inline code. Even calling Page.DataBind() in the Page_Load event handler does not evaluate the inilne code properly.

I'm sorry tusharashah, I should have been more specific. I don't want to say HomeANC.HRef= Request.ApplicationPath + "/Default.aspx";
I want to say href=" <<Request.ApplicationPath value>> + "/Default.aspx" ".

0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12217895

----------------------------------------------------------------------------------------------------------------------------------
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<html>
  <head>
    <title>test3</title>
    <script runat="server">
      public void Page_Load()
      {
            HomeANC.HRef= Request.ApplicationPath + "/Default.aspx";
      }
    </script>
 </head>
  <body MS_POSITIONING="GridLayout">
      
    <form id="Form1" method="post" runat="server">
      <a id="HomeANC" runat="server">Home</a>
     </form>
      
  </body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
This code works,
But, is this what you want?

-tushar
0
 
LVL 8

Author Comment

by:mppeters
ID: 12219405
Sorry no, it's not what I'm looking for.

I was looking to do something like:
<a id="HomeANC" runat="server" href="<%=Request.ApplicationPath%>/Default.aspx">Home</a>

But it turns out that this does not work.

What I've gone with instead is:
<asp:hyperlink id="HomeLNK" runat="server" navigateurl="~/Default.aspx" />

Also, the following would work:
<asp:hyperlink id="TrHomeLNK" runat="server" navigateurl='<%# Request.ApplicationPath + "/Default.aspx"%>' />
But in the Page_Load you must include: Page.DataBind()
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12411496
The question has been PAQ'd and the 125 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 60
ASP.NET 5 Templates 2 79
How do I prevent the pages in my site from showing up in a google search? 2 81
RLDC Reporting in Visual studio 11 17
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question