Chuck Le Butt
Chuck Le Butt

Reputation: 48758

Beginner question: Can I put an "if" statement between two <% %> tags in ASP.Net?

This is a real newbie question, I hope you can forgive me. I was wondering, can I put an if statement between two <% %> directly in an .aspx document? If so, how...?

The specific problem I'm having is this: I want to put the user's HTTP Referrer as a parameter in a link they click on (this must sound terribly counter-intuitive, but I have my reasons for doing it this way!).

So my problem is that sometimes Request.UrlReferrer returns a null value. To counter this, I was hoping to put something like:

<%# if(Request.UrlReferrer != null) { Server.UrlEncode(Request.UrlReferrer.ToString()) } %>

But it's not working... ("Error: Invalid expression term 'if'").

Thanks for any help!

Upvotes: 2

Views: 1592

Answers (2)

Ali Tarhini
Ali Tarhini

Reputation: 5358

any c# or vb .net code can be nested inside <% %> tags. in fact, if you create your pages in visual studio without code behind. you can write the entire code in these tags within the html

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062865

You can do:

<% if(Request.UrlReferrer != null) { %><%=Server.UrlEncode(Request.UrlReferrer.ToString())%><% } %>

or

<%=Request.UrlReferrer == null ? "" : Server.UrlEncode(Request.UrlReferrer.ToString()) %>

Upvotes: 9

Related Questions