Maninder
Maninder

Reputation: 1261

Use server tag in an anchor tag

I would like to use a property ID value in the anchor tag as below:

<a id="aExample" href="/test/example.aspx?id=<%# Id %>" runat="server">Example</a>

But when the page is rendered instead of getting the href as "/test/example.aspx?id=5" i am getting "/test/example.aspx?id=<%# Id %>" as plain text assigned as href of the anchor.

Id = is a property defined in code behind.

Can anybody help me whats wrong with this? N.B: I need runat="server"` to be present.

My tag is not inside any Grid view control but within a user control. <%=

Upvotes: 3

Views: 4595

Answers (6)

Akon
Akon

Reputation: 272

Please try this,

<a id="aExample" href='<%# string.Format("/test/example.aspx?id={0}", Id) %>'  runat="server">Example</a>

Upvotes: 0

JERRY-the chuha
JERRY-the chuha

Reputation: 582

You can also set the href of anchor tag in CodeBehind.

CodeBehind:

int Id = 4;
aExample.HRef = "/test/example.aspx?id=" + Id;

Thanks

Upvotes: 0

Dushyant
Dushyant

Reputation: 4960

Try this

<a id="aExample" href='<%= CompleteURL %>' runat="server">Example</a>

If you can build and bind the complete URL in your property (code behind). I think this can be optimal solution. This way you can also change your URL dynamically.

Upvotes: 0

Manish Sharma
Manish Sharma

Reputation: 2426

Try this,

if you want get variable value from your .cs so you can use. Declare variable in your page. .cs page

 public int Id = 0;

aspx page

<a id="aExample" href="/test/example.aspx?id=<%= Id %>" runat="server">Example</a>

and your a tag is inside in gridview control so you can use like...

<a id="aExample" href="/test/example.aspx?id=<%#Eval("Id")%>" runat="server">Example</a>

Upvotes: 1

tariq
tariq

Reputation: 2258

You can do this in code behind (if its not inside Gridview or some other control like that)

aExample.Href = "/test/example.aspx?id="+ YourEntity.Id;

Upvotes: 0

Hans Derks
Hans Derks

Reputation: 1027

Maybe you can try this

<asp:HyperLink ID="aExample" runat="server" NavigateUrl='<%# String.Format("/test/example.aspx?id={0}", Eval("Id")) %>'>Example</asp:HyperLink>

Upvotes: 0

Related Questions