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!
You can do:
<% if(Request.UrlReferrer != null) { %><%=Server.UrlEncode(Request.UrlReferrer.ToString())%><% } %>
or
<%=Request.UrlReferrer == null ? "" : Server.UrlEncode(Request.UrlReferrer.ToString()) %>
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
精彩评论