mppeters
asked on
Setting href of HtmlAnchor control (not in code-behind file)
I had the following regular html anchor tag defined in my user control.
<a href="<%=Request.Applicati onPath%>/D efault.asp x">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.Applicati onPath%>/D efault.asp x">Home</a >
This results in the link pointing to "http://localhost/mywebapp/myusercontrolsfolder/<%=Request.ApplicationPat h%>/Defaul t.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?
<a href="<%=Request.Applicati
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.Applicati
This results in the link pointing to "http://localhost/mywebapp/myusercontrolsfolder/<%=Request.ApplicationPat
Request.ApplicationPath no longer gets evaluated properly.
How the heck do I set the href without setting it in the Page_Load event handler?
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
--------------------------
<script runat="server">
public void Page_Load()
{
HomeANC.HRef= Request.ApplicationPath + "/Default.aspx";
}
</script>
--------------------------
(this code is cAsE sensitive)
-tushar
ASKER
>> As far as I know that server tags cannot contain <% ... %> constructs.
Sure they can.
Example:
<asp:Image Runat="server" Visible='<%# Convert.ToBoolean(DataBind er.Eval(Co ntainer.Da taItem, "IsActive")) %>' ImageUrl="_images/check.gi f" 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" ".
Sure they can.
Example:
<asp:Image Runat="server" Visible='<%# Convert.ToBoolean(DataBind
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" ".
--------------------------
<%@ 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
ASKER
Sorry no, it's not what I'm looking for.
I was looking to do something like:
<a id="HomeANC" runat="server" href="<%=Request.Applicati onPath%>/D efault.asp x">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.asp x" />
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()
I was looking to do something like:
<a id="HomeANC" runat="server" href="<%=Request.Applicati
But it turns out that this does not work.
What I've gone with instead is:
<asp:hyperlink id="HomeLNK" runat="server" navigateurl="~/Default.asp
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()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
-tushar