Sukhi Sukhi - 3 months ago 24
ASP.NET (C#) Question

href click problem if i add runat="server" attribute in asp.net

here is one href on aspx page

ASPX:

<a id="hrfReport" href="<%=sApplicationURL%>Reports.aspx">Report</a>


CODE BEHIND:

sApplicationURL = "http://localhost/";


now if i click on href page redirected to the reports page but if i add runat="server" attribute then href click not worked.actually i want to make it disable on particular condition.

Does anyone know how to do this?

Answer

You cannot use the <%=someVal%> construct in server tags. The compiler basically translates that to Response.Write(someVal).

You can use the <%#someVal%> construct, but you are required to DataBind() the value. For example:

ASPX:

<a id="hrfReport" href='<%#sApplicationURL + "Reports.aspx"%>' runat="server">Report</a>

CODE BEHIND:

sApplicationURL = "http://localhost/";
hrfReport.DataBind();

Alternatively...

If you have a server control, you can set its properties from the code behind as follows:

ASPX:

<a id="hrfReport" runat="server">Report</a>

CODE BEHIND:

hrfReport.HRef = sApplicationURL + "Reports.aspx";