开发者

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

开发者 https://www.devze.com 2023-01-26 12:27 出处:网络
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...?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消